fat_write.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100
  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, i;
  776. int ret = -1, name_len;
  777. char l_filename[VFAT_MAXLEN_BYTES];
  778. char bad[2] = " ";
  779. const char illegal[] = "<>:\"/\\|?*";
  780. *actwrite = size;
  781. dir_curclust = 0;
  782. if (read_bootsectandvi(&bs, &volinfo, &mydata->fatsize)) {
  783. debug("error: reading boot sector\n");
  784. return -1;
  785. }
  786. total_sector = bs.total_sect;
  787. if (total_sector == 0)
  788. total_sector = (int)cur_part_info.size; /* cast of lbaint_t */
  789. if (mydata->fatsize == 32)
  790. mydata->fatlength = bs.fat32_length;
  791. else
  792. mydata->fatlength = bs.fat_length;
  793. mydata->fat_sect = bs.reserved;
  794. cursect = mydata->rootdir_sect
  795. = mydata->fat_sect + mydata->fatlength * bs.fats;
  796. num_of_fats = bs.fats;
  797. mydata->sect_size = (bs.sector_size[1] << 8) + bs.sector_size[0];
  798. mydata->clust_size = bs.cluster_size;
  799. if (mydata->fatsize == 32) {
  800. mydata->data_begin = mydata->rootdir_sect -
  801. (mydata->clust_size * 2);
  802. } else {
  803. int rootdir_size;
  804. rootdir_size = ((bs.dir_entries[1] * (int)256 +
  805. bs.dir_entries[0]) *
  806. sizeof(dir_entry)) /
  807. mydata->sect_size;
  808. mydata->data_begin = mydata->rootdir_sect +
  809. rootdir_size -
  810. (mydata->clust_size * 2);
  811. }
  812. mydata->fatbufnum = -1;
  813. mydata->fat_dirty = 0;
  814. mydata->fatbuf = memalign(ARCH_DMA_MINALIGN, FATBUFSIZE);
  815. if (mydata->fatbuf == NULL) {
  816. debug("Error: allocating memory\n");
  817. return -1;
  818. }
  819. if (disk_read(cursect,
  820. (mydata->fatsize == 32) ?
  821. (mydata->clust_size) :
  822. PREFETCH_BLOCKS, do_fat_read_at_block) < 0) {
  823. debug("Error: reading rootdir block\n");
  824. goto exit;
  825. }
  826. dentptr = (dir_entry *) do_fat_read_at_block;
  827. /* Strip leading (back-)slashes */
  828. while ISDIRDELIM(*filename)
  829. ++filename;
  830. /* Check that the filename is valid */
  831. for (i = 0; i < strlen(illegal); ++i) {
  832. *bad = illegal[i];
  833. if (strstr(filename, bad)) {
  834. printf("FAT: illegal filename (%s)\n", filename);
  835. return -1;
  836. }
  837. }
  838. name_len = strlen(filename);
  839. if (name_len >= VFAT_MAXLEN_BYTES)
  840. name_len = VFAT_MAXLEN_BYTES - 1;
  841. memcpy(l_filename, filename, name_len);
  842. l_filename[name_len] = 0; /* terminate the string */
  843. downcase(l_filename, INT_MAX);
  844. startsect = mydata->rootdir_sect;
  845. retdent = find_directory_entry(mydata, startsect,
  846. l_filename, dentptr, 0);
  847. if (retdent) {
  848. /* Update file size and start_cluster in a directory entry */
  849. retdent->size = cpu_to_le32(size);
  850. start_cluster = START(retdent);
  851. if (start_cluster) {
  852. if (size) {
  853. ret = check_overflow(mydata, start_cluster,
  854. size);
  855. if (ret) {
  856. printf("Error: %llu overflow\n", size);
  857. goto exit;
  858. }
  859. }
  860. ret = clear_fatent(mydata, start_cluster);
  861. if (ret) {
  862. printf("Error: clearing FAT entries\n");
  863. goto exit;
  864. }
  865. if (!size)
  866. set_start_cluster(mydata, retdent, 0);
  867. } else if (size) {
  868. ret = start_cluster = find_empty_cluster(mydata);
  869. if (ret < 0) {
  870. printf("Error: finding empty cluster\n");
  871. goto exit;
  872. }
  873. ret = check_overflow(mydata, start_cluster, size);
  874. if (ret) {
  875. printf("Error: %llu overflow\n", size);
  876. goto exit;
  877. }
  878. set_start_cluster(mydata, retdent, start_cluster);
  879. }
  880. } else {
  881. /* Set short name to set alias checksum field in dir_slot */
  882. set_name(empty_dentptr, filename);
  883. fill_dir_slot(mydata, &empty_dentptr, filename);
  884. if (size) {
  885. ret = start_cluster = find_empty_cluster(mydata);
  886. if (ret < 0) {
  887. printf("Error: finding empty cluster\n");
  888. goto exit;
  889. }
  890. ret = check_overflow(mydata, start_cluster, size);
  891. if (ret) {
  892. printf("Error: %llu overflow\n", size);
  893. goto exit;
  894. }
  895. } else {
  896. start_cluster = 0;
  897. }
  898. /* Set attribute as archieve for regular file */
  899. fill_dentry(mydata, empty_dentptr, filename,
  900. start_cluster, size, 0x20);
  901. retdent = empty_dentptr;
  902. }
  903. ret = set_contents(mydata, retdent, buffer, size, actwrite);
  904. if (ret < 0) {
  905. printf("Error: writing contents\n");
  906. goto exit;
  907. }
  908. debug("attempt to write 0x%llx bytes\n", *actwrite);
  909. /* Flush fat buffer */
  910. ret = flush_dirty_fat_buffer(mydata);
  911. if (ret) {
  912. printf("Error: flush fat buffer\n");
  913. goto exit;
  914. }
  915. /* Write directory table to device */
  916. ret = set_cluster(mydata, dir_curclust, get_dentfromdir_block,
  917. mydata->clust_size * mydata->sect_size);
  918. if (ret)
  919. printf("Error: writing directory entry\n");
  920. exit:
  921. free(mydata->fatbuf);
  922. return ret;
  923. }
  924. int file_fat_write(const char *filename, void *buffer, loff_t offset,
  925. loff_t maxsize, loff_t *actwrite)
  926. {
  927. if (offset != 0) {
  928. printf("Error: non zero offset is currently not supported.\n");
  929. return -1;
  930. }
  931. printf("writing %s\n", filename);
  932. return do_fat_write(filename, buffer, maxsize, actwrite);
  933. }