fat_write.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098
  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. __u32 curclust = (startsect - mydata->data_begin) / mydata->clust_size;
  708. debug("get_dentfromdir: %s\n", filename);
  709. while (1) {
  710. dir_entry *dentptr;
  711. int i;
  712. if (get_cluster(mydata, curclust, get_dentfromdir_block,
  713. mydata->clust_size * mydata->sect_size) != 0) {
  714. printf("Error: reading directory block\n");
  715. return NULL;
  716. }
  717. dentptr = (dir_entry *)get_dentfromdir_block;
  718. dir_curclust = curclust;
  719. for (i = 0; i < DIRENTSPERCLUST; i++) {
  720. char s_name[14], l_name[VFAT_MAXLEN_BYTES];
  721. l_name[0] = '\0';
  722. if (dentptr->name[0] == DELETED_FLAG) {
  723. dentptr++;
  724. if (is_next_clust(mydata, dentptr))
  725. break;
  726. continue;
  727. }
  728. if ((dentptr->attr & ATTR_VOLUME)) {
  729. #ifdef CONFIG_SUPPORT_VFAT
  730. if ((dentptr->attr & ATTR_VFAT) &&
  731. (dentptr->name[0] & LAST_LONG_ENTRY_MASK)) {
  732. get_long_file_name(mydata, curclust,
  733. get_dentfromdir_block,
  734. &dentptr, l_name);
  735. debug("vfatname: |%s|\n", l_name);
  736. } else
  737. #endif
  738. {
  739. /* Volume label or VFAT entry */
  740. dentptr++;
  741. if (is_next_clust(mydata, dentptr))
  742. break;
  743. continue;
  744. }
  745. }
  746. if (dentptr->name[0] == 0) {
  747. debug("Dentname == NULL - %d\n", i);
  748. empty_dentptr = dentptr;
  749. return NULL;
  750. }
  751. get_name(dentptr, s_name);
  752. if (strcmp(filename, s_name)
  753. && strcmp(filename, l_name)) {
  754. debug("Mismatch: |%s|%s|\n",
  755. s_name, l_name);
  756. dentptr++;
  757. if (is_next_clust(mydata, dentptr))
  758. break;
  759. continue;
  760. }
  761. memcpy(retdent, dentptr, sizeof(dir_entry));
  762. debug("DentName: %s", s_name);
  763. debug(", start: 0x%x", START(dentptr));
  764. debug(", size: 0x%x %s\n",
  765. FAT2CPU32(dentptr->size),
  766. (dentptr->attr & ATTR_DIR) ?
  767. "(DIR)" : "");
  768. return dentptr;
  769. }
  770. curclust = get_fatent_value(mydata, dir_curclust);
  771. if ((curclust >= 0xffffff8) || (curclust >= 0xfff8)) {
  772. empty_dentptr = dentptr;
  773. return NULL;
  774. }
  775. if (CHECK_CLUST(curclust, mydata->fatsize)) {
  776. debug("curclust: 0x%x\n", curclust);
  777. debug("Invalid FAT entry\n");
  778. return NULL;
  779. }
  780. }
  781. return NULL;
  782. }
  783. static int do_fat_write(const char *filename, void *buffer,
  784. unsigned long size)
  785. {
  786. dir_entry *dentptr, *retdent;
  787. __u32 startsect;
  788. __u32 start_cluster;
  789. boot_sector bs;
  790. volume_info volinfo;
  791. fsdata datablock;
  792. fsdata *mydata = &datablock;
  793. int cursect;
  794. int ret = -1, name_len;
  795. char l_filename[VFAT_MAXLEN_BYTES];
  796. int write_size = size;
  797. dir_curclust = 0;
  798. if (read_bootsectandvi(&bs, &volinfo, &mydata->fatsize)) {
  799. debug("error: reading boot sector\n");
  800. return -1;
  801. }
  802. total_sector = bs.total_sect;
  803. if (total_sector == 0)
  804. total_sector = cur_part_info.size;
  805. if (mydata->fatsize == 32)
  806. mydata->fatlength = bs.fat32_length;
  807. else
  808. mydata->fatlength = bs.fat_length;
  809. mydata->fat_sect = bs.reserved;
  810. cursect = mydata->rootdir_sect
  811. = mydata->fat_sect + mydata->fatlength * bs.fats;
  812. num_of_fats = bs.fats;
  813. mydata->sect_size = (bs.sector_size[1] << 8) + bs.sector_size[0];
  814. mydata->clust_size = bs.cluster_size;
  815. if (mydata->fatsize == 32) {
  816. mydata->data_begin = mydata->rootdir_sect -
  817. (mydata->clust_size * 2);
  818. } else {
  819. int rootdir_size;
  820. rootdir_size = ((bs.dir_entries[1] * (int)256 +
  821. bs.dir_entries[0]) *
  822. sizeof(dir_entry)) /
  823. mydata->sect_size;
  824. mydata->data_begin = mydata->rootdir_sect +
  825. rootdir_size -
  826. (mydata->clust_size * 2);
  827. }
  828. mydata->fatbufnum = -1;
  829. mydata->fatbuf = malloc(FATBUFSIZE);
  830. if (mydata->fatbuf == NULL) {
  831. debug("Error: allocating memory\n");
  832. return -1;
  833. }
  834. if (disk_read(cursect,
  835. (mydata->fatsize == 32) ?
  836. (mydata->clust_size) :
  837. PREFETCH_BLOCKS, do_fat_read_block) < 0) {
  838. debug("Error: reading rootdir block\n");
  839. goto exit;
  840. }
  841. dentptr = (dir_entry *) do_fat_read_block;
  842. name_len = strlen(filename);
  843. if (name_len >= VFAT_MAXLEN_BYTES)
  844. name_len = VFAT_MAXLEN_BYTES - 1;
  845. memcpy(l_filename, filename, name_len);
  846. l_filename[name_len] = 0; /* terminate the string */
  847. downcase(l_filename);
  848. startsect = mydata->rootdir_sect;
  849. retdent = find_directory_entry(mydata, startsect,
  850. l_filename, dentptr, 0);
  851. if (retdent) {
  852. /* Update file size and start_cluster in a directory entry */
  853. retdent->size = cpu_to_le32(size);
  854. start_cluster = FAT2CPU16(retdent->start);
  855. if (mydata->fatsize == 32)
  856. start_cluster |=
  857. (FAT2CPU16(retdent->starthi) << 16);
  858. ret = check_overflow(mydata, start_cluster, size);
  859. if (ret) {
  860. printf("Error: %ld overflow\n", size);
  861. goto exit;
  862. }
  863. ret = clear_fatent(mydata, start_cluster);
  864. if (ret) {
  865. printf("Error: clearing FAT entries\n");
  866. goto exit;
  867. }
  868. ret = set_contents(mydata, retdent, buffer, size);
  869. if (ret < 0) {
  870. printf("Error: writing contents\n");
  871. goto exit;
  872. }
  873. write_size = ret;
  874. debug("attempt to write 0x%x bytes\n", write_size);
  875. /* Flush fat buffer */
  876. ret = flush_fat_buffer(mydata);
  877. if (ret) {
  878. printf("Error: flush fat buffer\n");
  879. goto exit;
  880. }
  881. /* Write directory table to device */
  882. ret = set_cluster(mydata, dir_curclust,
  883. get_dentfromdir_block,
  884. mydata->clust_size * mydata->sect_size);
  885. if (ret) {
  886. printf("Error: writing directory entry\n");
  887. goto exit;
  888. }
  889. } else {
  890. /* Set short name to set alias checksum field in dir_slot */
  891. set_name(empty_dentptr, filename);
  892. fill_dir_slot(mydata, &empty_dentptr, filename);
  893. ret = start_cluster = find_empty_cluster(mydata);
  894. if (ret < 0) {
  895. printf("Error: finding empty cluster\n");
  896. goto exit;
  897. }
  898. ret = check_overflow(mydata, start_cluster, size);
  899. if (ret) {
  900. printf("Error: %ld overflow\n", size);
  901. goto exit;
  902. }
  903. /* Set attribute as archieve for regular file */
  904. fill_dentry(mydata, empty_dentptr, filename,
  905. start_cluster, size, 0x20);
  906. ret = set_contents(mydata, empty_dentptr, buffer, size);
  907. if (ret < 0) {
  908. printf("Error: writing contents\n");
  909. goto exit;
  910. }
  911. write_size = ret;
  912. debug("attempt to write 0x%x bytes\n", write_size);
  913. /* Flush fat buffer */
  914. ret = flush_fat_buffer(mydata);
  915. if (ret) {
  916. printf("Error: flush fat buffer\n");
  917. goto exit;
  918. }
  919. /* Write directory table to device */
  920. ret = set_cluster(mydata, dir_curclust,
  921. get_dentfromdir_block,
  922. mydata->clust_size * mydata->sect_size);
  923. if (ret) {
  924. printf("Error: writing directory entry\n");
  925. goto exit;
  926. }
  927. }
  928. exit:
  929. free(mydata->fatbuf);
  930. return ret < 0 ? ret : write_size;
  931. }
  932. int file_fat_write(const char *filename, void *buffer, unsigned long maxsize)
  933. {
  934. printf("writing %s\n", filename);
  935. return do_fat_write(filename, buffer, maxsize);
  936. }