fat_write.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106
  1. /*
  2. * fat_write.c
  3. *
  4. * R/W (V)FAT 12/16/32 filesystem implementation by Donggeun Kim
  5. *
  6. * SPDX-License-Identifier: GPL-2.0+
  7. */
  8. #include <common.h>
  9. #include <command.h>
  10. #include <config.h>
  11. #include <fat.h>
  12. #include <asm/byteorder.h>
  13. #include <part.h>
  14. #include <linux/ctype.h>
  15. #include "fat.c"
  16. static void uppercase(char *str, int len)
  17. {
  18. int i;
  19. for (i = 0; i < len; i++) {
  20. *str = toupper(*str);
  21. str++;
  22. }
  23. }
  24. static int total_sector;
  25. static int disk_write(__u32 block, __u32 nr_blocks, void *buf)
  26. {
  27. if (!cur_dev || !cur_dev->block_write)
  28. return -1;
  29. if (cur_part_info.start + block + nr_blocks >
  30. cur_part_info.start + total_sector) {
  31. printf("error: overflow occurs\n");
  32. return -1;
  33. }
  34. return cur_dev->block_write(cur_dev->dev,
  35. cur_part_info.start + block, nr_blocks, buf);
  36. }
  37. /*
  38. * Set short name in directory entry
  39. */
  40. static void set_name(dir_entry *dirent, const char *filename)
  41. {
  42. char s_name[VFAT_MAXLEN_BYTES];
  43. char *period;
  44. int period_location, len, i, ext_num;
  45. if (filename == NULL)
  46. return;
  47. len = strlen(filename);
  48. if (len == 0)
  49. return;
  50. strcpy(s_name, filename);
  51. uppercase(s_name, len);
  52. period = strchr(s_name, '.');
  53. if (period == NULL) {
  54. period_location = len;
  55. ext_num = 0;
  56. } else {
  57. period_location = period - s_name;
  58. ext_num = len - period_location - 1;
  59. }
  60. /* Pad spaces when the length of file name is shorter than eight */
  61. if (period_location < 8) {
  62. memcpy(dirent->name, s_name, period_location);
  63. for (i = period_location; i < 8; i++)
  64. dirent->name[i] = ' ';
  65. } else if (period_location == 8) {
  66. memcpy(dirent->name, s_name, period_location);
  67. } else {
  68. memcpy(dirent->name, s_name, 6);
  69. dirent->name[6] = '~';
  70. dirent->name[7] = '1';
  71. }
  72. if (ext_num < 3) {
  73. memcpy(dirent->ext, s_name + period_location + 1, ext_num);
  74. for (i = ext_num; i < 3; i++)
  75. dirent->ext[i] = ' ';
  76. } else
  77. memcpy(dirent->ext, s_name + period_location + 1, 3);
  78. debug("name : %s\n", dirent->name);
  79. debug("ext : %s\n", dirent->ext);
  80. }
  81. static __u8 num_of_fats;
  82. /*
  83. * Write fat buffer into block device
  84. */
  85. static int flush_fat_buffer(fsdata *mydata)
  86. {
  87. int getsize = FATBUFBLOCKS;
  88. __u32 fatlength = mydata->fatlength;
  89. __u8 *bufptr = mydata->fatbuf;
  90. __u32 startblock = mydata->fatbufnum * FATBUFBLOCKS;
  91. startblock += mydata->fat_sect;
  92. if (getsize > fatlength)
  93. getsize = fatlength;
  94. /* Write FAT buf */
  95. if (disk_write(startblock, getsize, bufptr) < 0) {
  96. debug("error: writing FAT blocks\n");
  97. return -1;
  98. }
  99. if (num_of_fats == 2) {
  100. /* Update corresponding second FAT blocks */
  101. startblock += mydata->fatlength;
  102. if (disk_write(startblock, getsize, bufptr) < 0) {
  103. debug("error: writing second FAT blocks\n");
  104. return -1;
  105. }
  106. }
  107. return 0;
  108. }
  109. /*
  110. * Get the entry at index 'entry' in a FAT (12/16/32) table.
  111. * On failure 0x00 is returned.
  112. * When bufnum is changed, write back the previous fatbuf to the disk.
  113. */
  114. static __u32 get_fatent_value(fsdata *mydata, __u32 entry)
  115. {
  116. __u32 bufnum;
  117. __u32 off16, offset;
  118. __u32 ret = 0x00;
  119. __u16 val1, val2;
  120. if (CHECK_CLUST(entry, mydata->fatsize)) {
  121. printf("Error: Invalid FAT entry: 0x%08x\n", entry);
  122. return ret;
  123. }
  124. switch (mydata->fatsize) {
  125. case 32:
  126. bufnum = entry / FAT32BUFSIZE;
  127. offset = entry - bufnum * FAT32BUFSIZE;
  128. break;
  129. case 16:
  130. bufnum = entry / FAT16BUFSIZE;
  131. offset = entry - bufnum * FAT16BUFSIZE;
  132. break;
  133. case 12:
  134. bufnum = entry / FAT12BUFSIZE;
  135. offset = entry - bufnum * FAT12BUFSIZE;
  136. break;
  137. default:
  138. /* Unsupported FAT size */
  139. return ret;
  140. }
  141. debug("FAT%d: entry: 0x%04x = %d, offset: 0x%04x = %d\n",
  142. mydata->fatsize, entry, entry, offset, offset);
  143. /* Read a new block of FAT entries into the cache. */
  144. if (bufnum != mydata->fatbufnum) {
  145. int getsize = FATBUFBLOCKS;
  146. __u8 *bufptr = mydata->fatbuf;
  147. __u32 fatlength = mydata->fatlength;
  148. __u32 startblock = bufnum * FATBUFBLOCKS;
  149. if (getsize > fatlength)
  150. getsize = fatlength;
  151. fatlength *= mydata->sect_size; /* We want it in bytes now */
  152. startblock += mydata->fat_sect; /* Offset from start of disk */
  153. /* Write back the fatbuf to the disk */
  154. if (mydata->fatbufnum != -1) {
  155. if (flush_fat_buffer(mydata) < 0)
  156. return -1;
  157. }
  158. if (disk_read(startblock, getsize, bufptr) < 0) {
  159. debug("Error reading FAT blocks\n");
  160. return ret;
  161. }
  162. mydata->fatbufnum = bufnum;
  163. }
  164. /* Get the actual entry from the table */
  165. switch (mydata->fatsize) {
  166. case 32:
  167. ret = FAT2CPU32(((__u32 *) mydata->fatbuf)[offset]);
  168. break;
  169. case 16:
  170. ret = FAT2CPU16(((__u16 *) mydata->fatbuf)[offset]);
  171. break;
  172. case 12:
  173. off16 = (offset * 3) / 4;
  174. switch (offset & 0x3) {
  175. case 0:
  176. ret = FAT2CPU16(((__u16 *) mydata->fatbuf)[off16]);
  177. ret &= 0xfff;
  178. break;
  179. case 1:
  180. val1 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]);
  181. val1 &= 0xf000;
  182. val2 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16 + 1]);
  183. val2 &= 0x00ff;
  184. ret = (val2 << 4) | (val1 >> 12);
  185. break;
  186. case 2:
  187. val1 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]);
  188. val1 &= 0xff00;
  189. val2 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16 + 1]);
  190. val2 &= 0x000f;
  191. ret = (val2 << 8) | (val1 >> 8);
  192. break;
  193. case 3:
  194. ret = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]);
  195. ret = (ret & 0xfff0) >> 4;
  196. break;
  197. default:
  198. break;
  199. }
  200. break;
  201. }
  202. debug("FAT%d: ret: %08x, entry: %08x, offset: %04x\n",
  203. mydata->fatsize, ret, entry, offset);
  204. return ret;
  205. }
  206. /*
  207. * Set the file name information from 'name' into 'slotptr',
  208. */
  209. static int str2slot(dir_slot *slotptr, const char *name, int *idx)
  210. {
  211. int j, end_idx = 0;
  212. for (j = 0; j <= 8; j += 2) {
  213. if (name[*idx] == 0x00) {
  214. slotptr->name0_4[j] = 0;
  215. slotptr->name0_4[j + 1] = 0;
  216. end_idx++;
  217. goto name0_4;
  218. }
  219. slotptr->name0_4[j] = name[*idx];
  220. (*idx)++;
  221. end_idx++;
  222. }
  223. for (j = 0; j <= 10; j += 2) {
  224. if (name[*idx] == 0x00) {
  225. slotptr->name5_10[j] = 0;
  226. slotptr->name5_10[j + 1] = 0;
  227. end_idx++;
  228. goto name5_10;
  229. }
  230. slotptr->name5_10[j] = name[*idx];
  231. (*idx)++;
  232. end_idx++;
  233. }
  234. for (j = 0; j <= 2; j += 2) {
  235. if (name[*idx] == 0x00) {
  236. slotptr->name11_12[j] = 0;
  237. slotptr->name11_12[j + 1] = 0;
  238. end_idx++;
  239. goto name11_12;
  240. }
  241. slotptr->name11_12[j] = name[*idx];
  242. (*idx)++;
  243. end_idx++;
  244. }
  245. if (name[*idx] == 0x00)
  246. return 1;
  247. return 0;
  248. /* Not used characters are filled with 0xff 0xff */
  249. name0_4:
  250. for (; end_idx < 5; end_idx++) {
  251. slotptr->name0_4[end_idx * 2] = 0xff;
  252. slotptr->name0_4[end_idx * 2 + 1] = 0xff;
  253. }
  254. end_idx = 5;
  255. name5_10:
  256. end_idx -= 5;
  257. for (; end_idx < 6; end_idx++) {
  258. slotptr->name5_10[end_idx * 2] = 0xff;
  259. slotptr->name5_10[end_idx * 2 + 1] = 0xff;
  260. }
  261. end_idx = 11;
  262. name11_12:
  263. end_idx -= 11;
  264. for (; end_idx < 2; end_idx++) {
  265. slotptr->name11_12[end_idx * 2] = 0xff;
  266. slotptr->name11_12[end_idx * 2 + 1] = 0xff;
  267. }
  268. return 1;
  269. }
  270. static int is_next_clust(fsdata *mydata, dir_entry *dentptr);
  271. static void flush_dir_table(fsdata *mydata, dir_entry **dentptr);
  272. /*
  273. * Fill dir_slot entries with appropriate name, id, and attr
  274. * The real directory entry is returned by 'dentptr'
  275. */
  276. static void
  277. fill_dir_slot(fsdata *mydata, dir_entry **dentptr, const char *l_name)
  278. {
  279. dir_slot *slotptr = (dir_slot *)get_contents_vfatname_block;
  280. __u8 counter = 0, checksum;
  281. int idx = 0, ret;
  282. char s_name[16];
  283. /* Get short file name and checksum value */
  284. strncpy(s_name, (*dentptr)->name, 16);
  285. checksum = mkcksum((*dentptr)->name, (*dentptr)->ext);
  286. do {
  287. memset(slotptr, 0x00, sizeof(dir_slot));
  288. ret = str2slot(slotptr, l_name, &idx);
  289. slotptr->id = ++counter;
  290. slotptr->attr = ATTR_VFAT;
  291. slotptr->alias_checksum = checksum;
  292. slotptr++;
  293. } while (ret == 0);
  294. slotptr--;
  295. slotptr->id |= LAST_LONG_ENTRY_MASK;
  296. while (counter >= 1) {
  297. if (is_next_clust(mydata, *dentptr)) {
  298. /* A new cluster is allocated for directory table */
  299. flush_dir_table(mydata, dentptr);
  300. }
  301. memcpy(*dentptr, slotptr, sizeof(dir_slot));
  302. (*dentptr)++;
  303. slotptr--;
  304. counter--;
  305. }
  306. if (is_next_clust(mydata, *dentptr)) {
  307. /* A new cluster is allocated for directory table */
  308. flush_dir_table(mydata, dentptr);
  309. }
  310. }
  311. static __u32 dir_curclust;
  312. /*
  313. * Extract the full long filename starting at 'retdent' (which is really
  314. * a slot) into 'l_name'. If successful also copy the real directory entry
  315. * into 'retdent'
  316. * If additional adjacent cluster for directory entries is read into memory,
  317. * then 'get_contents_vfatname_block' is copied into 'get_dentfromdir_block' and
  318. * the location of the real directory entry is returned by 'retdent'
  319. * Return 0 on success, -1 otherwise.
  320. */
  321. static int
  322. get_long_file_name(fsdata *mydata, int curclust, __u8 *cluster,
  323. dir_entry **retdent, char *l_name)
  324. {
  325. dir_entry *realdent;
  326. dir_slot *slotptr = (dir_slot *)(*retdent);
  327. dir_slot *slotptr2 = NULL;
  328. __u8 *buflimit = cluster + mydata->sect_size * ((curclust == 0) ?
  329. PREFETCH_BLOCKS :
  330. mydata->clust_size);
  331. __u8 counter = (slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff;
  332. int idx = 0, cur_position = 0;
  333. if (counter > VFAT_MAXSEQ) {
  334. debug("Error: VFAT name is too long\n");
  335. return -1;
  336. }
  337. while ((__u8 *)slotptr < buflimit) {
  338. if (counter == 0)
  339. break;
  340. if (((slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff) != counter)
  341. return -1;
  342. slotptr++;
  343. counter--;
  344. }
  345. if ((__u8 *)slotptr >= buflimit) {
  346. if (curclust == 0)
  347. return -1;
  348. curclust = get_fatent_value(mydata, dir_curclust);
  349. if (CHECK_CLUST(curclust, mydata->fatsize)) {
  350. debug("curclust: 0x%x\n", curclust);
  351. printf("Invalid FAT entry\n");
  352. return -1;
  353. }
  354. dir_curclust = curclust;
  355. if (get_cluster(mydata, curclust, get_contents_vfatname_block,
  356. mydata->clust_size * mydata->sect_size) != 0) {
  357. debug("Error: reading directory block\n");
  358. return -1;
  359. }
  360. slotptr2 = (dir_slot *)get_contents_vfatname_block;
  361. while (counter > 0) {
  362. if (((slotptr2->id & ~LAST_LONG_ENTRY_MASK)
  363. & 0xff) != counter)
  364. return -1;
  365. slotptr2++;
  366. counter--;
  367. }
  368. /* Save the real directory entry */
  369. realdent = (dir_entry *)slotptr2;
  370. while ((__u8 *)slotptr2 > get_contents_vfatname_block) {
  371. slotptr2--;
  372. slot2str(slotptr2, l_name, &idx);
  373. }
  374. } else {
  375. /* Save the real directory entry */
  376. realdent = (dir_entry *)slotptr;
  377. }
  378. do {
  379. slotptr--;
  380. if (slot2str(slotptr, l_name, &idx))
  381. break;
  382. } while (!(slotptr->id & LAST_LONG_ENTRY_MASK));
  383. l_name[idx] = '\0';
  384. if (*l_name == DELETED_FLAG)
  385. *l_name = '\0';
  386. else if (*l_name == aRING)
  387. *l_name = DELETED_FLAG;
  388. downcase(l_name);
  389. /* Return the real directory entry */
  390. *retdent = realdent;
  391. if (slotptr2) {
  392. memcpy(get_dentfromdir_block, get_contents_vfatname_block,
  393. mydata->clust_size * mydata->sect_size);
  394. cur_position = (__u8 *)realdent - get_contents_vfatname_block;
  395. *retdent = (dir_entry *) &get_dentfromdir_block[cur_position];
  396. }
  397. return 0;
  398. }
  399. /*
  400. * Set the entry at index 'entry' in a FAT (16/32) table.
  401. */
  402. static int set_fatent_value(fsdata *mydata, __u32 entry, __u32 entry_value)
  403. {
  404. __u32 bufnum, offset;
  405. switch (mydata->fatsize) {
  406. case 32:
  407. bufnum = entry / FAT32BUFSIZE;
  408. offset = entry - bufnum * FAT32BUFSIZE;
  409. break;
  410. case 16:
  411. bufnum = entry / FAT16BUFSIZE;
  412. offset = entry - bufnum * FAT16BUFSIZE;
  413. break;
  414. default:
  415. /* Unsupported FAT size */
  416. return -1;
  417. }
  418. /* Read a new block of FAT entries into the cache. */
  419. if (bufnum != mydata->fatbufnum) {
  420. int getsize = FATBUFBLOCKS;
  421. __u8 *bufptr = mydata->fatbuf;
  422. __u32 fatlength = mydata->fatlength;
  423. __u32 startblock = bufnum * FATBUFBLOCKS;
  424. fatlength *= mydata->sect_size;
  425. startblock += mydata->fat_sect;
  426. if (getsize > fatlength)
  427. getsize = fatlength;
  428. if (mydata->fatbufnum != -1) {
  429. if (flush_fat_buffer(mydata) < 0)
  430. return -1;
  431. }
  432. if (disk_read(startblock, getsize, bufptr) < 0) {
  433. debug("Error reading FAT blocks\n");
  434. return -1;
  435. }
  436. mydata->fatbufnum = bufnum;
  437. }
  438. /* Set the actual entry */
  439. switch (mydata->fatsize) {
  440. case 32:
  441. ((__u32 *) mydata->fatbuf)[offset] = cpu_to_le32(entry_value);
  442. break;
  443. case 16:
  444. ((__u16 *) mydata->fatbuf)[offset] = cpu_to_le16(entry_value);
  445. break;
  446. default:
  447. return -1;
  448. }
  449. return 0;
  450. }
  451. /*
  452. * Determine the entry value at index 'entry' in a FAT (16/32) table
  453. */
  454. static __u32 determine_fatent(fsdata *mydata, __u32 entry)
  455. {
  456. __u32 next_fat, next_entry = entry + 1;
  457. while (1) {
  458. next_fat = get_fatent_value(mydata, next_entry);
  459. if (next_fat == 0) {
  460. set_fatent_value(mydata, entry, next_entry);
  461. break;
  462. }
  463. next_entry++;
  464. }
  465. debug("FAT%d: entry: %08x, entry_value: %04x\n",
  466. mydata->fatsize, entry, next_entry);
  467. return next_entry;
  468. }
  469. /*
  470. * Write at most 'size' bytes from 'buffer' into the specified cluster.
  471. * Return 0 on success, -1 otherwise.
  472. */
  473. static int
  474. set_cluster(fsdata *mydata, __u32 clustnum, __u8 *buffer,
  475. unsigned long size)
  476. {
  477. int idx = 0;
  478. __u32 startsect;
  479. if (clustnum > 0)
  480. startsect = mydata->data_begin +
  481. clustnum * mydata->clust_size;
  482. else
  483. startsect = mydata->rootdir_sect;
  484. debug("clustnum: %d, startsect: %d\n", clustnum, startsect);
  485. if ((size / mydata->sect_size) > 0) {
  486. if (disk_write(startsect, size / mydata->sect_size, buffer) < 0) {
  487. debug("Error writing data\n");
  488. return -1;
  489. }
  490. }
  491. if (size % mydata->sect_size) {
  492. __u8 tmpbuf[mydata->sect_size];
  493. idx = size / mydata->sect_size;
  494. buffer += idx * mydata->sect_size;
  495. memcpy(tmpbuf, buffer, size % mydata->sect_size);
  496. if (disk_write(startsect + idx, 1, tmpbuf) < 0) {
  497. debug("Error writing data\n");
  498. return -1;
  499. }
  500. return 0;
  501. }
  502. return 0;
  503. }
  504. /*
  505. * Find the first empty cluster
  506. */
  507. static int find_empty_cluster(fsdata *mydata)
  508. {
  509. __u32 fat_val, entry = 3;
  510. while (1) {
  511. fat_val = get_fatent_value(mydata, entry);
  512. if (fat_val == 0)
  513. break;
  514. entry++;
  515. }
  516. return entry;
  517. }
  518. /*
  519. * Write directory entries in 'get_dentfromdir_block' to block device
  520. */
  521. static void flush_dir_table(fsdata *mydata, dir_entry **dentptr)
  522. {
  523. int dir_newclust = 0;
  524. if (set_cluster(mydata, dir_curclust,
  525. get_dentfromdir_block,
  526. mydata->clust_size * mydata->sect_size) != 0) {
  527. printf("error: wrinting directory entry\n");
  528. return;
  529. }
  530. dir_newclust = find_empty_cluster(mydata);
  531. set_fatent_value(mydata, dir_curclust, dir_newclust);
  532. if (mydata->fatsize == 32)
  533. set_fatent_value(mydata, dir_newclust, 0xffffff8);
  534. else if (mydata->fatsize == 16)
  535. set_fatent_value(mydata, dir_newclust, 0xfff8);
  536. dir_curclust = dir_newclust;
  537. if (flush_fat_buffer(mydata) < 0)
  538. return;
  539. memset(get_dentfromdir_block, 0x00,
  540. mydata->clust_size * mydata->sect_size);
  541. *dentptr = (dir_entry *) get_dentfromdir_block;
  542. }
  543. /*
  544. * Set empty cluster from 'entry' to the end of a file
  545. */
  546. static int clear_fatent(fsdata *mydata, __u32 entry)
  547. {
  548. __u32 fat_val;
  549. while (1) {
  550. fat_val = get_fatent_value(mydata, entry);
  551. if (fat_val != 0)
  552. set_fatent_value(mydata, entry, 0);
  553. else
  554. break;
  555. if (fat_val == 0xfffffff || fat_val == 0xffff)
  556. break;
  557. entry = fat_val;
  558. }
  559. /* Flush fat buffer */
  560. if (flush_fat_buffer(mydata) < 0)
  561. return -1;
  562. return 0;
  563. }
  564. /*
  565. * Write at most 'maxsize' bytes from 'buffer' into
  566. * the file associated with 'dentptr'
  567. * Return the number of bytes read or -1 on fatal errors.
  568. */
  569. static int
  570. set_contents(fsdata *mydata, dir_entry *dentptr, __u8 *buffer,
  571. unsigned long maxsize)
  572. {
  573. unsigned long filesize = FAT2CPU32(dentptr->size), gotsize = 0;
  574. unsigned int bytesperclust = mydata->clust_size * mydata->sect_size;
  575. __u32 curclust = START(dentptr);
  576. __u32 endclust = 0, newclust = 0;
  577. unsigned long actsize;
  578. debug("Filesize: %ld bytes\n", filesize);
  579. if (maxsize > 0 && filesize > maxsize)
  580. filesize = maxsize;
  581. debug("%ld bytes\n", filesize);
  582. actsize = bytesperclust;
  583. endclust = curclust;
  584. do {
  585. /* search for consecutive clusters */
  586. while (actsize < filesize) {
  587. newclust = determine_fatent(mydata, endclust);
  588. if ((newclust - 1) != endclust)
  589. goto getit;
  590. if (CHECK_CLUST(newclust, mydata->fatsize)) {
  591. debug("curclust: 0x%x\n", newclust);
  592. debug("Invalid FAT entry\n");
  593. return gotsize;
  594. }
  595. endclust = newclust;
  596. actsize += bytesperclust;
  597. }
  598. /* actsize >= file size */
  599. actsize -= bytesperclust;
  600. /* set remaining clusters */
  601. if (set_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
  602. debug("error: writing cluster\n");
  603. return -1;
  604. }
  605. /* set remaining bytes */
  606. gotsize += (int)actsize;
  607. filesize -= actsize;
  608. buffer += actsize;
  609. actsize = filesize;
  610. if (set_cluster(mydata, endclust, buffer, (int)actsize) != 0) {
  611. debug("error: writing cluster\n");
  612. return -1;
  613. }
  614. gotsize += actsize;
  615. /* Mark end of file in FAT */
  616. if (mydata->fatsize == 16)
  617. newclust = 0xffff;
  618. else if (mydata->fatsize == 32)
  619. newclust = 0xfffffff;
  620. set_fatent_value(mydata, endclust, newclust);
  621. return gotsize;
  622. getit:
  623. if (set_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
  624. debug("error: writing cluster\n");
  625. return -1;
  626. }
  627. gotsize += (int)actsize;
  628. filesize -= actsize;
  629. buffer += actsize;
  630. if (CHECK_CLUST(curclust, mydata->fatsize)) {
  631. debug("curclust: 0x%x\n", curclust);
  632. debug("Invalid FAT entry\n");
  633. return gotsize;
  634. }
  635. actsize = bytesperclust;
  636. curclust = endclust = newclust;
  637. } while (1);
  638. }
  639. /*
  640. * Fill dir_entry
  641. */
  642. static void fill_dentry(fsdata *mydata, dir_entry *dentptr,
  643. const char *filename, __u32 start_cluster, __u32 size, __u8 attr)
  644. {
  645. if (mydata->fatsize == 32)
  646. dentptr->starthi =
  647. cpu_to_le16((start_cluster & 0xffff0000) >> 16);
  648. dentptr->start = cpu_to_le16(start_cluster & 0xffff);
  649. dentptr->size = cpu_to_le32(size);
  650. dentptr->attr = attr;
  651. set_name(dentptr, filename);
  652. }
  653. /*
  654. * Check whether adding a file makes the file system to
  655. * exceed the size of the block device
  656. * Return -1 when overflow occurs, otherwise return 0
  657. */
  658. static int check_overflow(fsdata *mydata, __u32 clustnum, unsigned long size)
  659. {
  660. __u32 startsect, sect_num;
  661. if (clustnum > 0) {
  662. startsect = mydata->data_begin +
  663. clustnum * mydata->clust_size;
  664. } else {
  665. startsect = mydata->rootdir_sect;
  666. }
  667. sect_num = size / mydata->sect_size;
  668. if (size % mydata->sect_size)
  669. sect_num++;
  670. if (startsect + sect_num > cur_part_info.start + total_sector)
  671. return -1;
  672. return 0;
  673. }
  674. /*
  675. * Check if adding several entries exceed one cluster boundary
  676. */
  677. static int is_next_clust(fsdata *mydata, dir_entry *dentptr)
  678. {
  679. int cur_position;
  680. cur_position = (__u8 *)dentptr - get_dentfromdir_block;
  681. if (cur_position >= mydata->clust_size * mydata->sect_size)
  682. return 1;
  683. else
  684. return 0;
  685. }
  686. static dir_entry *empty_dentptr;
  687. /*
  688. * Find a directory entry based on filename or start cluster number
  689. * If the directory entry is not found,
  690. * the new position for writing a directory entry will be returned
  691. */
  692. static dir_entry *find_directory_entry(fsdata *mydata, int startsect,
  693. char *filename, dir_entry *retdent, __u32 start)
  694. {
  695. __u32 curclust = (startsect - mydata->data_begin) / mydata->clust_size;
  696. debug("get_dentfromdir: %s\n", filename);
  697. while (1) {
  698. dir_entry *dentptr;
  699. int i;
  700. if (get_cluster(mydata, curclust, get_dentfromdir_block,
  701. mydata->clust_size * mydata->sect_size) != 0) {
  702. printf("Error: reading directory block\n");
  703. return NULL;
  704. }
  705. dentptr = (dir_entry *)get_dentfromdir_block;
  706. dir_curclust = curclust;
  707. for (i = 0; i < DIRENTSPERCLUST; i++) {
  708. char s_name[14], l_name[VFAT_MAXLEN_BYTES];
  709. l_name[0] = '\0';
  710. if (dentptr->name[0] == DELETED_FLAG) {
  711. dentptr++;
  712. if (is_next_clust(mydata, dentptr))
  713. break;
  714. continue;
  715. }
  716. if ((dentptr->attr & ATTR_VOLUME)) {
  717. if (vfat_enabled &&
  718. (dentptr->attr & ATTR_VFAT) &&
  719. (dentptr->name[0] & LAST_LONG_ENTRY_MASK)) {
  720. get_long_file_name(mydata, curclust,
  721. get_dentfromdir_block,
  722. &dentptr, l_name);
  723. debug("vfatname: |%s|\n", l_name);
  724. } else {
  725. /* Volume label or VFAT entry */
  726. dentptr++;
  727. if (is_next_clust(mydata, dentptr))
  728. break;
  729. continue;
  730. }
  731. }
  732. if (dentptr->name[0] == 0) {
  733. debug("Dentname == NULL - %d\n", i);
  734. empty_dentptr = dentptr;
  735. return NULL;
  736. }
  737. get_name(dentptr, s_name);
  738. if (strcmp(filename, s_name)
  739. && strcmp(filename, l_name)) {
  740. debug("Mismatch: |%s|%s|\n",
  741. s_name, l_name);
  742. dentptr++;
  743. if (is_next_clust(mydata, dentptr))
  744. break;
  745. continue;
  746. }
  747. memcpy(retdent, dentptr, sizeof(dir_entry));
  748. debug("DentName: %s", s_name);
  749. debug(", start: 0x%x", START(dentptr));
  750. debug(", size: 0x%x %s\n",
  751. FAT2CPU32(dentptr->size),
  752. (dentptr->attr & ATTR_DIR) ?
  753. "(DIR)" : "");
  754. return dentptr;
  755. }
  756. /*
  757. * In FAT16/12, the root dir is locate before data area, shows
  758. * in following:
  759. * -------------------------------------------------------------
  760. * | Boot | FAT1 & 2 | Root dir | Data (start from cluster #2) |
  761. * -------------------------------------------------------------
  762. *
  763. * As a result if curclust is in Root dir, it is a negative
  764. * number or 0, 1.
  765. *
  766. */
  767. if (mydata->fatsize != 32 && (int)curclust <= 1) {
  768. /* Current clust is in root dir, set to next clust */
  769. curclust++;
  770. if ((int)curclust <= 1)
  771. continue; /* continue to find */
  772. /* Reach the end of root dir */
  773. empty_dentptr = dentptr;
  774. return NULL;
  775. }
  776. curclust = get_fatent_value(mydata, dir_curclust);
  777. if (IS_LAST_CLUST(curclust, mydata->fatsize)) {
  778. empty_dentptr = dentptr;
  779. return NULL;
  780. }
  781. if (CHECK_CLUST(curclust, mydata->fatsize)) {
  782. debug("curclust: 0x%x\n", curclust);
  783. debug("Invalid FAT entry\n");
  784. return NULL;
  785. }
  786. }
  787. return NULL;
  788. }
  789. static int do_fat_write(const char *filename, void *buffer,
  790. unsigned long size)
  791. {
  792. dir_entry *dentptr, *retdent;
  793. __u32 startsect;
  794. __u32 start_cluster;
  795. boot_sector bs;
  796. volume_info volinfo;
  797. fsdata datablock;
  798. fsdata *mydata = &datablock;
  799. int cursect;
  800. int ret = -1, name_len;
  801. char l_filename[VFAT_MAXLEN_BYTES];
  802. int write_size = size;
  803. dir_curclust = 0;
  804. if (read_bootsectandvi(&bs, &volinfo, &mydata->fatsize)) {
  805. debug("error: reading boot sector\n");
  806. return -1;
  807. }
  808. total_sector = bs.total_sect;
  809. if (total_sector == 0)
  810. total_sector = (int)cur_part_info.size; /* cast of lbaint_t */
  811. if (mydata->fatsize == 32)
  812. mydata->fatlength = bs.fat32_length;
  813. else
  814. mydata->fatlength = bs.fat_length;
  815. mydata->fat_sect = bs.reserved;
  816. cursect = mydata->rootdir_sect
  817. = mydata->fat_sect + mydata->fatlength * bs.fats;
  818. num_of_fats = bs.fats;
  819. mydata->sect_size = (bs.sector_size[1] << 8) + bs.sector_size[0];
  820. mydata->clust_size = bs.cluster_size;
  821. if (mydata->fatsize == 32) {
  822. mydata->data_begin = mydata->rootdir_sect -
  823. (mydata->clust_size * 2);
  824. } else {
  825. int rootdir_size;
  826. rootdir_size = ((bs.dir_entries[1] * (int)256 +
  827. bs.dir_entries[0]) *
  828. sizeof(dir_entry)) /
  829. mydata->sect_size;
  830. mydata->data_begin = mydata->rootdir_sect +
  831. rootdir_size -
  832. (mydata->clust_size * 2);
  833. }
  834. mydata->fatbufnum = -1;
  835. mydata->fatbuf = memalign(ARCH_DMA_MINALIGN, FATBUFSIZE);
  836. if (mydata->fatbuf == NULL) {
  837. debug("Error: allocating memory\n");
  838. return -1;
  839. }
  840. if (disk_read(cursect,
  841. (mydata->fatsize == 32) ?
  842. (mydata->clust_size) :
  843. PREFETCH_BLOCKS, do_fat_read_at_block) < 0) {
  844. debug("Error: reading rootdir block\n");
  845. goto exit;
  846. }
  847. dentptr = (dir_entry *) do_fat_read_at_block;
  848. name_len = strlen(filename);
  849. if (name_len >= VFAT_MAXLEN_BYTES)
  850. name_len = VFAT_MAXLEN_BYTES - 1;
  851. memcpy(l_filename, filename, name_len);
  852. l_filename[name_len] = 0; /* terminate the string */
  853. downcase(l_filename);
  854. startsect = mydata->rootdir_sect;
  855. retdent = find_directory_entry(mydata, startsect,
  856. l_filename, dentptr, 0);
  857. if (retdent) {
  858. /* Update file size and start_cluster in a directory entry */
  859. retdent->size = cpu_to_le32(size);
  860. start_cluster = FAT2CPU16(retdent->start);
  861. if (mydata->fatsize == 32)
  862. start_cluster |=
  863. (FAT2CPU16(retdent->starthi) << 16);
  864. ret = check_overflow(mydata, start_cluster, size);
  865. if (ret) {
  866. printf("Error: %ld overflow\n", size);
  867. goto exit;
  868. }
  869. ret = clear_fatent(mydata, start_cluster);
  870. if (ret) {
  871. printf("Error: clearing FAT entries\n");
  872. goto exit;
  873. }
  874. ret = set_contents(mydata, retdent, buffer, size);
  875. if (ret < 0) {
  876. printf("Error: writing contents\n");
  877. goto exit;
  878. }
  879. write_size = ret;
  880. debug("attempt to write 0x%x bytes\n", write_size);
  881. /* Flush fat buffer */
  882. ret = flush_fat_buffer(mydata);
  883. if (ret) {
  884. printf("Error: flush fat buffer\n");
  885. goto exit;
  886. }
  887. /* Write directory table to device */
  888. ret = set_cluster(mydata, dir_curclust,
  889. get_dentfromdir_block,
  890. mydata->clust_size * mydata->sect_size);
  891. if (ret) {
  892. printf("Error: writing directory entry\n");
  893. goto exit;
  894. }
  895. } else {
  896. /* Set short name to set alias checksum field in dir_slot */
  897. set_name(empty_dentptr, filename);
  898. fill_dir_slot(mydata, &empty_dentptr, filename);
  899. ret = start_cluster = find_empty_cluster(mydata);
  900. if (ret < 0) {
  901. printf("Error: finding empty cluster\n");
  902. goto exit;
  903. }
  904. ret = check_overflow(mydata, start_cluster, size);
  905. if (ret) {
  906. printf("Error: %ld overflow\n", size);
  907. goto exit;
  908. }
  909. /* Set attribute as archieve for regular file */
  910. fill_dentry(mydata, empty_dentptr, filename,
  911. start_cluster, size, 0x20);
  912. ret = set_contents(mydata, empty_dentptr, buffer, size);
  913. if (ret < 0) {
  914. printf("Error: writing contents\n");
  915. goto exit;
  916. }
  917. write_size = ret;
  918. debug("attempt to write 0x%x bytes\n", write_size);
  919. /* Flush fat buffer */
  920. ret = flush_fat_buffer(mydata);
  921. if (ret) {
  922. printf("Error: flush fat buffer\n");
  923. goto exit;
  924. }
  925. /* Write directory table to device */
  926. ret = set_cluster(mydata, dir_curclust,
  927. get_dentfromdir_block,
  928. mydata->clust_size * mydata->sect_size);
  929. if (ret) {
  930. printf("Error: writing directory entry\n");
  931. goto exit;
  932. }
  933. }
  934. exit:
  935. free(mydata->fatbuf);
  936. return ret < 0 ? ret : write_size;
  937. }
  938. int file_fat_write(const char *filename, void *buffer, unsigned long maxsize)
  939. {
  940. printf("writing %s\n", filename);
  941. return do_fat_write(filename, buffer, maxsize);
  942. }