fat_write.c 25 KB

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