fat_write.c 25 KB

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