fat_write.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090
  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);
  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 = mydata->data_begin +
  435. clustnum * mydata->clust_size;
  436. else
  437. startsect = mydata->rootdir_sect;
  438. debug("clustnum: %d, startsect: %d\n", clustnum, startsect);
  439. if ((unsigned long)buffer & (ARCH_DMA_MINALIGN - 1)) {
  440. ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
  441. debug("FAT: Misaligned buffer address (%p)\n", buffer);
  442. while (size >= mydata->sect_size) {
  443. memcpy(tmpbuf, buffer, mydata->sect_size);
  444. ret = disk_write(startsect++, 1, tmpbuf);
  445. if (ret != 1) {
  446. debug("Error writing data (got %d)\n", ret);
  447. return -1;
  448. }
  449. buffer += mydata->sect_size;
  450. size -= mydata->sect_size;
  451. }
  452. } else if (size >= mydata->sect_size) {
  453. idx = size / mydata->sect_size;
  454. ret = disk_write(startsect, idx, buffer);
  455. if (ret != idx) {
  456. debug("Error writing data (got %d)\n", ret);
  457. return -1;
  458. }
  459. startsect += idx;
  460. idx *= mydata->sect_size;
  461. buffer += idx;
  462. size -= idx;
  463. }
  464. if (size) {
  465. ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
  466. memcpy(tmpbuf, buffer, size);
  467. ret = disk_write(startsect, 1, tmpbuf);
  468. if (ret != 1) {
  469. debug("Error writing data (got %d)\n", ret);
  470. return -1;
  471. }
  472. }
  473. return 0;
  474. }
  475. /*
  476. * Find the first empty cluster
  477. */
  478. static int find_empty_cluster(fsdata *mydata)
  479. {
  480. __u32 fat_val, entry = 3;
  481. while (1) {
  482. fat_val = get_fatent(mydata, entry);
  483. if (fat_val == 0)
  484. break;
  485. entry++;
  486. }
  487. return entry;
  488. }
  489. /*
  490. * Write directory entries in 'get_dentfromdir_block' to block device
  491. */
  492. static void flush_dir_table(fsdata *mydata, dir_entry **dentptr)
  493. {
  494. int dir_newclust = 0;
  495. if (set_cluster(mydata, dir_curclust,
  496. get_dentfromdir_block,
  497. mydata->clust_size * mydata->sect_size) != 0) {
  498. printf("error: wrinting directory entry\n");
  499. return;
  500. }
  501. dir_newclust = find_empty_cluster(mydata);
  502. set_fatent_value(mydata, dir_curclust, dir_newclust);
  503. if (mydata->fatsize == 32)
  504. set_fatent_value(mydata, dir_newclust, 0xffffff8);
  505. else if (mydata->fatsize == 16)
  506. set_fatent_value(mydata, dir_newclust, 0xfff8);
  507. else if (mydata->fatsize == 12)
  508. set_fatent_value(mydata, dir_newclust, 0xff8);
  509. dir_curclust = dir_newclust;
  510. if (flush_dirty_fat_buffer(mydata) < 0)
  511. return;
  512. memset(get_dentfromdir_block, 0x00,
  513. mydata->clust_size * mydata->sect_size);
  514. *dentptr = (dir_entry *) get_dentfromdir_block;
  515. }
  516. /*
  517. * Set empty cluster from 'entry' to the end of a file
  518. */
  519. static int clear_fatent(fsdata *mydata, __u32 entry)
  520. {
  521. __u32 fat_val;
  522. while (!CHECK_CLUST(entry, mydata->fatsize)) {
  523. fat_val = get_fatent(mydata, entry);
  524. if (fat_val != 0)
  525. set_fatent_value(mydata, entry, 0);
  526. else
  527. break;
  528. entry = fat_val;
  529. }
  530. /* Flush fat buffer */
  531. if (flush_dirty_fat_buffer(mydata) < 0)
  532. return -1;
  533. return 0;
  534. }
  535. /*
  536. * Write at most 'maxsize' bytes from 'buffer' into
  537. * the file associated with 'dentptr'
  538. * Update the number of bytes written in *gotsize and return 0
  539. * or return -1 on fatal errors.
  540. */
  541. static int
  542. set_contents(fsdata *mydata, dir_entry *dentptr, __u8 *buffer,
  543. loff_t maxsize, loff_t *gotsize)
  544. {
  545. loff_t filesize = FAT2CPU32(dentptr->size);
  546. unsigned int bytesperclust = mydata->clust_size * mydata->sect_size;
  547. __u32 curclust = START(dentptr);
  548. __u32 endclust = 0, newclust = 0;
  549. loff_t actsize;
  550. *gotsize = 0;
  551. debug("Filesize: %llu bytes\n", filesize);
  552. if (maxsize > 0 && filesize > maxsize)
  553. filesize = maxsize;
  554. debug("%llu bytes\n", filesize);
  555. if (!curclust) {
  556. if (filesize) {
  557. debug("error: nonempty clusterless file!\n");
  558. return -1;
  559. }
  560. return 0;
  561. }
  562. actsize = bytesperclust;
  563. endclust = curclust;
  564. do {
  565. /* search for consecutive clusters */
  566. while (actsize < filesize) {
  567. newclust = determine_fatent(mydata, endclust);
  568. if ((newclust - 1) != endclust)
  569. goto getit;
  570. if (CHECK_CLUST(newclust, mydata->fatsize)) {
  571. debug("newclust: 0x%x\n", newclust);
  572. debug("Invalid FAT entry\n");
  573. return 0;
  574. }
  575. endclust = newclust;
  576. actsize += bytesperclust;
  577. }
  578. /* set remaining bytes */
  579. actsize = filesize;
  580. if (set_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
  581. debug("error: writing cluster\n");
  582. return -1;
  583. }
  584. *gotsize += actsize;
  585. /* Mark end of file in FAT */
  586. if (mydata->fatsize == 12)
  587. newclust = 0xfff;
  588. else if (mydata->fatsize == 16)
  589. newclust = 0xffff;
  590. else if (mydata->fatsize == 32)
  591. newclust = 0xfffffff;
  592. set_fatent_value(mydata, endclust, newclust);
  593. return 0;
  594. getit:
  595. if (set_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
  596. debug("error: writing cluster\n");
  597. return -1;
  598. }
  599. *gotsize += actsize;
  600. filesize -= actsize;
  601. buffer += actsize;
  602. if (CHECK_CLUST(newclust, mydata->fatsize)) {
  603. debug("newclust: 0x%x\n", newclust);
  604. debug("Invalid FAT entry\n");
  605. return 0;
  606. }
  607. actsize = bytesperclust;
  608. curclust = endclust = newclust;
  609. } while (1);
  610. }
  611. /*
  612. * Set start cluster in directory entry
  613. */
  614. static void set_start_cluster(const fsdata *mydata, dir_entry *dentptr,
  615. __u32 start_cluster)
  616. {
  617. if (mydata->fatsize == 32)
  618. dentptr->starthi =
  619. cpu_to_le16((start_cluster & 0xffff0000) >> 16);
  620. dentptr->start = cpu_to_le16(start_cluster & 0xffff);
  621. }
  622. /*
  623. * Fill dir_entry
  624. */
  625. static void fill_dentry(fsdata *mydata, dir_entry *dentptr,
  626. const char *filename, __u32 start_cluster, __u32 size, __u8 attr)
  627. {
  628. set_start_cluster(mydata, dentptr, start_cluster);
  629. dentptr->size = cpu_to_le32(size);
  630. dentptr->attr = attr;
  631. set_name(dentptr, filename);
  632. }
  633. /*
  634. * Check whether adding a file makes the file system to
  635. * exceed the size of the block device
  636. * Return -1 when overflow occurs, otherwise return 0
  637. */
  638. static int check_overflow(fsdata *mydata, __u32 clustnum, loff_t size)
  639. {
  640. __u32 startsect, sect_num, offset;
  641. if (clustnum > 0) {
  642. startsect = mydata->data_begin +
  643. clustnum * mydata->clust_size;
  644. } else {
  645. startsect = mydata->rootdir_sect;
  646. }
  647. sect_num = div_u64_rem(size, mydata->sect_size, &offset);
  648. if (offset != 0)
  649. sect_num++;
  650. if (startsect + sect_num > cur_part_info.start + total_sector)
  651. return -1;
  652. return 0;
  653. }
  654. /*
  655. * Check if adding several entries exceed one cluster boundary
  656. */
  657. static int is_next_clust(fsdata *mydata, dir_entry *dentptr)
  658. {
  659. int cur_position;
  660. cur_position = (__u8 *)dentptr - get_dentfromdir_block;
  661. if (cur_position >= mydata->clust_size * mydata->sect_size)
  662. return 1;
  663. else
  664. return 0;
  665. }
  666. static dir_entry *empty_dentptr;
  667. /*
  668. * Find a directory entry based on filename or start cluster number
  669. * If the directory entry is not found,
  670. * the new position for writing a directory entry will be returned
  671. */
  672. static dir_entry *find_directory_entry(fsdata *mydata, int startsect,
  673. char *filename, dir_entry *retdent, __u32 start)
  674. {
  675. __u32 curclust = (startsect - mydata->data_begin) / mydata->clust_size;
  676. debug("get_dentfromdir: %s\n", filename);
  677. while (1) {
  678. dir_entry *dentptr;
  679. int i;
  680. if (get_cluster(mydata, curclust, get_dentfromdir_block,
  681. mydata->clust_size * mydata->sect_size) != 0) {
  682. printf("Error: reading directory block\n");
  683. return NULL;
  684. }
  685. dentptr = (dir_entry *)get_dentfromdir_block;
  686. dir_curclust = curclust;
  687. for (i = 0; i < DIRENTSPERCLUST; i++) {
  688. char s_name[14], l_name[VFAT_MAXLEN_BYTES];
  689. l_name[0] = '\0';
  690. if (dentptr->name[0] == DELETED_FLAG) {
  691. dentptr++;
  692. if (is_next_clust(mydata, dentptr))
  693. break;
  694. continue;
  695. }
  696. if ((dentptr->attr & ATTR_VOLUME)) {
  697. if (vfat_enabled &&
  698. (dentptr->attr & ATTR_VFAT) &&
  699. (dentptr->name[0] & LAST_LONG_ENTRY_MASK)) {
  700. get_long_file_name(mydata, curclust,
  701. get_dentfromdir_block,
  702. &dentptr, l_name);
  703. debug("vfatname: |%s|\n", l_name);
  704. } else {
  705. /* Volume label or VFAT entry */
  706. dentptr++;
  707. if (is_next_clust(mydata, dentptr))
  708. break;
  709. continue;
  710. }
  711. }
  712. if (dentptr->name[0] == 0) {
  713. debug("Dentname == NULL - %d\n", i);
  714. empty_dentptr = dentptr;
  715. return NULL;
  716. }
  717. get_name(dentptr, s_name);
  718. if (strcmp(filename, s_name)
  719. && strcmp(filename, l_name)) {
  720. debug("Mismatch: |%s|%s|\n",
  721. s_name, l_name);
  722. dentptr++;
  723. if (is_next_clust(mydata, dentptr))
  724. break;
  725. continue;
  726. }
  727. memcpy(retdent, dentptr, sizeof(dir_entry));
  728. debug("DentName: %s", s_name);
  729. debug(", start: 0x%x", START(dentptr));
  730. debug(", size: 0x%x %s\n",
  731. FAT2CPU32(dentptr->size),
  732. (dentptr->attr & ATTR_DIR) ?
  733. "(DIR)" : "");
  734. return dentptr;
  735. }
  736. /*
  737. * In FAT16/12, the root dir is locate before data area, shows
  738. * in following:
  739. * -------------------------------------------------------------
  740. * | Boot | FAT1 & 2 | Root dir | Data (start from cluster #2) |
  741. * -------------------------------------------------------------
  742. *
  743. * As a result if curclust is in Root dir, it is a negative
  744. * number or 0, 1.
  745. *
  746. */
  747. if (mydata->fatsize != 32 && (int)curclust <= 1) {
  748. /* Current clust is in root dir, set to next clust */
  749. curclust++;
  750. if ((int)curclust <= 1)
  751. continue; /* continue to find */
  752. /* Reach the end of root dir */
  753. empty_dentptr = dentptr;
  754. return NULL;
  755. }
  756. curclust = get_fatent(mydata, dir_curclust);
  757. if (IS_LAST_CLUST(curclust, mydata->fatsize)) {
  758. empty_dentptr = dentptr;
  759. return NULL;
  760. }
  761. if (CHECK_CLUST(curclust, mydata->fatsize)) {
  762. debug("curclust: 0x%x\n", curclust);
  763. debug("Invalid FAT entry\n");
  764. return NULL;
  765. }
  766. }
  767. return NULL;
  768. }
  769. static int do_fat_write(const char *filename, void *buffer, loff_t size,
  770. loff_t *actwrite)
  771. {
  772. dir_entry *dentptr, *retdent;
  773. __u32 startsect;
  774. __u32 start_cluster;
  775. boot_sector bs;
  776. volume_info volinfo;
  777. fsdata datablock;
  778. fsdata *mydata = &datablock;
  779. int cursect;
  780. int ret = -1, name_len;
  781. char l_filename[VFAT_MAXLEN_BYTES];
  782. *actwrite = size;
  783. dir_curclust = 0;
  784. if (read_bootsectandvi(&bs, &volinfo, &mydata->fatsize)) {
  785. debug("error: reading boot sector\n");
  786. return -1;
  787. }
  788. total_sector = bs.total_sect;
  789. if (total_sector == 0)
  790. total_sector = (int)cur_part_info.size; /* cast of lbaint_t */
  791. if (mydata->fatsize == 32)
  792. mydata->fatlength = bs.fat32_length;
  793. else
  794. mydata->fatlength = bs.fat_length;
  795. mydata->fat_sect = bs.reserved;
  796. cursect = mydata->rootdir_sect
  797. = mydata->fat_sect + mydata->fatlength * bs.fats;
  798. num_of_fats = bs.fats;
  799. mydata->sect_size = (bs.sector_size[1] << 8) + bs.sector_size[0];
  800. mydata->clust_size = bs.cluster_size;
  801. if (mydata->fatsize == 32) {
  802. mydata->data_begin = mydata->rootdir_sect -
  803. (mydata->clust_size * 2);
  804. } else {
  805. int rootdir_size;
  806. rootdir_size = ((bs.dir_entries[1] * (int)256 +
  807. bs.dir_entries[0]) *
  808. sizeof(dir_entry)) /
  809. mydata->sect_size;
  810. mydata->data_begin = mydata->rootdir_sect +
  811. rootdir_size -
  812. (mydata->clust_size * 2);
  813. }
  814. mydata->fatbufnum = -1;
  815. mydata->fat_dirty = 0;
  816. mydata->fatbuf = memalign(ARCH_DMA_MINALIGN, FATBUFSIZE);
  817. if (mydata->fatbuf == NULL) {
  818. debug("Error: allocating memory\n");
  819. return -1;
  820. }
  821. if (disk_read(cursect,
  822. (mydata->fatsize == 32) ?
  823. (mydata->clust_size) :
  824. PREFETCH_BLOCKS, do_fat_read_at_block) < 0) {
  825. debug("Error: reading rootdir block\n");
  826. goto exit;
  827. }
  828. dentptr = (dir_entry *) do_fat_read_at_block;
  829. name_len = strlen(filename);
  830. if (name_len >= VFAT_MAXLEN_BYTES)
  831. name_len = VFAT_MAXLEN_BYTES - 1;
  832. memcpy(l_filename, filename, name_len);
  833. l_filename[name_len] = 0; /* terminate the string */
  834. downcase(l_filename);
  835. startsect = mydata->rootdir_sect;
  836. retdent = find_directory_entry(mydata, startsect,
  837. l_filename, dentptr, 0);
  838. if (retdent) {
  839. /* Update file size and start_cluster in a directory entry */
  840. retdent->size = cpu_to_le32(size);
  841. start_cluster = START(retdent);
  842. if (start_cluster) {
  843. if (size) {
  844. ret = check_overflow(mydata, start_cluster,
  845. size);
  846. if (ret) {
  847. printf("Error: %llu overflow\n", size);
  848. goto exit;
  849. }
  850. }
  851. ret = clear_fatent(mydata, start_cluster);
  852. if (ret) {
  853. printf("Error: clearing FAT entries\n");
  854. goto exit;
  855. }
  856. if (!size)
  857. set_start_cluster(mydata, retdent, 0);
  858. } else if (size) {
  859. ret = start_cluster = find_empty_cluster(mydata);
  860. if (ret < 0) {
  861. printf("Error: finding empty cluster\n");
  862. goto exit;
  863. }
  864. ret = check_overflow(mydata, start_cluster, size);
  865. if (ret) {
  866. printf("Error: %llu overflow\n", size);
  867. goto exit;
  868. }
  869. set_start_cluster(mydata, retdent, start_cluster);
  870. }
  871. } else {
  872. /* Set short name to set alias checksum field in dir_slot */
  873. set_name(empty_dentptr, filename);
  874. fill_dir_slot(mydata, &empty_dentptr, filename);
  875. if (size) {
  876. ret = start_cluster = find_empty_cluster(mydata);
  877. if (ret < 0) {
  878. printf("Error: finding empty cluster\n");
  879. goto exit;
  880. }
  881. ret = check_overflow(mydata, start_cluster, size);
  882. if (ret) {
  883. printf("Error: %llu overflow\n", size);
  884. goto exit;
  885. }
  886. } else {
  887. start_cluster = 0;
  888. }
  889. /* Set attribute as archieve for regular file */
  890. fill_dentry(mydata, empty_dentptr, filename,
  891. start_cluster, size, 0x20);
  892. retdent = empty_dentptr;
  893. }
  894. ret = set_contents(mydata, retdent, buffer, size, actwrite);
  895. if (ret < 0) {
  896. printf("Error: writing contents\n");
  897. goto exit;
  898. }
  899. debug("attempt to write 0x%llx bytes\n", *actwrite);
  900. /* Flush fat buffer */
  901. ret = flush_dirty_fat_buffer(mydata);
  902. if (ret) {
  903. printf("Error: flush fat buffer\n");
  904. goto exit;
  905. }
  906. /* Write directory table to device */
  907. ret = set_cluster(mydata, dir_curclust, get_dentfromdir_block,
  908. mydata->clust_size * mydata->sect_size);
  909. if (ret)
  910. printf("Error: writing directory entry\n");
  911. exit:
  912. free(mydata->fatbuf);
  913. return ret;
  914. }
  915. int file_fat_write(const char *filename, void *buffer, loff_t offset,
  916. loff_t maxsize, loff_t *actwrite)
  917. {
  918. if (offset != 0) {
  919. printf("Error: non zero offset is currently not supported.\n");
  920. return -1;
  921. }
  922. printf("writing %s\n", filename);
  923. return do_fat_write(filename, buffer, maxsize, actwrite);
  924. }