fat_write.c 25 KB

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