fat_write.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094
  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 <linux/ctype.h>
  31. #include "fat.c"
  32. static void uppercase(char *str, int len)
  33. {
  34. int i;
  35. for (i = 0; i < len; i++) {
  36. *str = toupper(*str);
  37. str++;
  38. }
  39. }
  40. static int total_sector;
  41. static int disk_write(__u32 block, __u32 nr_blocks, void *buf)
  42. {
  43. if (!cur_dev || !cur_dev->block_write)
  44. return -1;
  45. if (cur_part_info.start + block + nr_blocks >
  46. cur_part_info.start + total_sector) {
  47. printf("error: overflow occurs\n");
  48. return -1;
  49. }
  50. return cur_dev->block_write(cur_dev->dev,
  51. cur_part_info.start + block, nr_blocks, buf);
  52. }
  53. /*
  54. * Set short name in directory entry
  55. */
  56. static void set_name(dir_entry *dirent, const char *filename)
  57. {
  58. char s_name[VFAT_MAXLEN_BYTES];
  59. char *period;
  60. int period_location, len, i, ext_num;
  61. if (filename == NULL)
  62. return;
  63. len = strlen(filename);
  64. if (len == 0)
  65. return;
  66. memcpy(s_name, filename, len);
  67. uppercase(s_name, len);
  68. period = strchr(s_name, '.');
  69. if (period == NULL) {
  70. period_location = len;
  71. ext_num = 0;
  72. } else {
  73. period_location = period - s_name;
  74. ext_num = len - period_location - 1;
  75. }
  76. /* Pad spaces when the length of file name is shorter than eight */
  77. if (period_location < 8) {
  78. memcpy(dirent->name, s_name, period_location);
  79. for (i = period_location; i < 8; i++)
  80. dirent->name[i] = ' ';
  81. } else if (period_location == 8) {
  82. memcpy(dirent->name, s_name, period_location);
  83. } else {
  84. memcpy(dirent->name, s_name, 6);
  85. dirent->name[6] = '~';
  86. dirent->name[7] = '1';
  87. }
  88. if (ext_num < 3) {
  89. memcpy(dirent->ext, s_name + period_location + 1, ext_num);
  90. for (i = ext_num; i < 3; i++)
  91. dirent->ext[i] = ' ';
  92. } else
  93. memcpy(dirent->ext, s_name + period_location + 1, 3);
  94. debug("name : %s\n", dirent->name);
  95. debug("ext : %s\n", dirent->ext);
  96. }
  97. static __u8 num_of_fats;
  98. /*
  99. * Write fat buffer into block device
  100. */
  101. static int flush_fat_buffer(fsdata *mydata)
  102. {
  103. int getsize = FATBUFBLOCKS;
  104. __u32 fatlength = mydata->fatlength;
  105. __u8 *bufptr = mydata->fatbuf;
  106. __u32 startblock = mydata->fatbufnum * FATBUFBLOCKS;
  107. fatlength *= mydata->sect_size;
  108. startblock += mydata->fat_sect;
  109. if (getsize > fatlength)
  110. getsize = fatlength;
  111. /* Write FAT buf */
  112. if (disk_write(startblock, getsize, bufptr) < 0) {
  113. debug("error: writing FAT blocks\n");
  114. return -1;
  115. }
  116. if (num_of_fats == 2) {
  117. /* Update corresponding second FAT blocks */
  118. startblock += mydata->fatlength;
  119. if (disk_write(startblock, getsize, bufptr) < 0) {
  120. debug("error: writing second FAT blocks\n");
  121. return -1;
  122. }
  123. }
  124. return 0;
  125. }
  126. /*
  127. * Get the entry at index 'entry' in a FAT (12/16/32) table.
  128. * On failure 0x00 is returned.
  129. * When bufnum is changed, write back the previous fatbuf to the disk.
  130. */
  131. static __u32 get_fatent_value(fsdata *mydata, __u32 entry)
  132. {
  133. __u32 bufnum;
  134. __u32 off16, offset;
  135. __u32 ret = 0x00;
  136. __u16 val1, val2;
  137. switch (mydata->fatsize) {
  138. case 32:
  139. bufnum = entry / FAT32BUFSIZE;
  140. offset = entry - bufnum * FAT32BUFSIZE;
  141. break;
  142. case 16:
  143. bufnum = entry / FAT16BUFSIZE;
  144. offset = entry - bufnum * FAT16BUFSIZE;
  145. break;
  146. case 12:
  147. bufnum = entry / FAT12BUFSIZE;
  148. offset = entry - bufnum * FAT12BUFSIZE;
  149. break;
  150. default:
  151. /* Unsupported FAT size */
  152. return ret;
  153. }
  154. debug("FAT%d: entry: 0x%04x = %d, offset: 0x%04x = %d\n",
  155. mydata->fatsize, entry, entry, offset, offset);
  156. /* Read a new block of FAT entries into the cache. */
  157. if (bufnum != mydata->fatbufnum) {
  158. int getsize = FATBUFBLOCKS;
  159. __u8 *bufptr = mydata->fatbuf;
  160. __u32 fatlength = mydata->fatlength;
  161. __u32 startblock = bufnum * FATBUFBLOCKS;
  162. if (getsize > fatlength)
  163. getsize = fatlength;
  164. fatlength *= mydata->sect_size; /* We want it in bytes now */
  165. startblock += mydata->fat_sect; /* Offset from start of disk */
  166. /* Write back the fatbuf to the disk */
  167. if (mydata->fatbufnum != -1) {
  168. if (flush_fat_buffer(mydata) < 0)
  169. return -1;
  170. }
  171. if (disk_read(startblock, getsize, bufptr) < 0) {
  172. debug("Error reading FAT blocks\n");
  173. return ret;
  174. }
  175. mydata->fatbufnum = bufnum;
  176. }
  177. /* Get the actual entry from the table */
  178. switch (mydata->fatsize) {
  179. case 32:
  180. ret = FAT2CPU32(((__u32 *) mydata->fatbuf)[offset]);
  181. break;
  182. case 16:
  183. ret = FAT2CPU16(((__u16 *) mydata->fatbuf)[offset]);
  184. break;
  185. case 12:
  186. off16 = (offset * 3) / 4;
  187. switch (offset & 0x3) {
  188. case 0:
  189. ret = FAT2CPU16(((__u16 *) mydata->fatbuf)[off16]);
  190. ret &= 0xfff;
  191. break;
  192. case 1:
  193. val1 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]);
  194. val1 &= 0xf000;
  195. val2 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16 + 1]);
  196. val2 &= 0x00ff;
  197. ret = (val2 << 4) | (val1 >> 12);
  198. break;
  199. case 2:
  200. val1 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]);
  201. val1 &= 0xff00;
  202. val2 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16 + 1]);
  203. val2 &= 0x000f;
  204. ret = (val2 << 8) | (val1 >> 8);
  205. break;
  206. case 3:
  207. ret = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]);
  208. ret = (ret & 0xfff0) >> 4;
  209. break;
  210. default:
  211. break;
  212. }
  213. break;
  214. }
  215. debug("FAT%d: ret: %08x, entry: %08x, offset: %04x\n",
  216. mydata->fatsize, ret, entry, offset);
  217. return ret;
  218. }
  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_contents_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((*dentptr)->name, (*dentptr)->ext);
  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_contents_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_contents_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_contents_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_contents_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_contents_vfatname_block,
  406. mydata->clust_size * mydata->sect_size);
  407. cur_position = (__u8 *)realdent - get_contents_vfatname_block;
  408. *retdent = (dir_entry *) &get_dentfromdir_block[cur_position];
  409. }
  410. return 0;
  411. }
  412. /*
  413. * Set the entry at index 'entry' in a FAT (16/32) table.
  414. */
  415. static int set_fatent_value(fsdata *mydata, __u32 entry, __u32 entry_value)
  416. {
  417. __u32 bufnum, offset;
  418. switch (mydata->fatsize) {
  419. case 32:
  420. bufnum = entry / FAT32BUFSIZE;
  421. offset = entry - bufnum * FAT32BUFSIZE;
  422. break;
  423. case 16:
  424. bufnum = entry / FAT16BUFSIZE;
  425. offset = entry - bufnum * FAT16BUFSIZE;
  426. break;
  427. default:
  428. /* Unsupported FAT size */
  429. return -1;
  430. }
  431. /* Read a new block of FAT entries into the cache. */
  432. if (bufnum != mydata->fatbufnum) {
  433. int getsize = FATBUFBLOCKS;
  434. __u8 *bufptr = mydata->fatbuf;
  435. __u32 fatlength = mydata->fatlength;
  436. __u32 startblock = bufnum * FATBUFBLOCKS;
  437. fatlength *= mydata->sect_size;
  438. startblock += mydata->fat_sect;
  439. if (getsize > fatlength)
  440. getsize = fatlength;
  441. if (mydata->fatbufnum != -1) {
  442. if (flush_fat_buffer(mydata) < 0)
  443. return -1;
  444. }
  445. if (disk_read(startblock, getsize, bufptr) < 0) {
  446. debug("Error reading FAT blocks\n");
  447. return -1;
  448. }
  449. mydata->fatbufnum = bufnum;
  450. }
  451. /* Set the actual entry */
  452. switch (mydata->fatsize) {
  453. case 32:
  454. ((__u32 *) mydata->fatbuf)[offset] = cpu_to_le32(entry_value);
  455. break;
  456. case 16:
  457. ((__u16 *) mydata->fatbuf)[offset] = cpu_to_le16(entry_value);
  458. break;
  459. default:
  460. return -1;
  461. }
  462. return 0;
  463. }
  464. /*
  465. * Determine the entry value at index 'entry' in a FAT (16/32) table
  466. */
  467. static __u32 determine_fatent(fsdata *mydata, __u32 entry)
  468. {
  469. __u32 next_fat, next_entry = entry + 1;
  470. while (1) {
  471. next_fat = get_fatent_value(mydata, next_entry);
  472. if (next_fat == 0) {
  473. set_fatent_value(mydata, entry, next_entry);
  474. break;
  475. }
  476. next_entry++;
  477. }
  478. debug("FAT%d: entry: %08x, entry_value: %04x\n",
  479. mydata->fatsize, entry, next_entry);
  480. return next_entry;
  481. }
  482. /*
  483. * Write at most 'size' bytes from 'buffer' into the specified cluster.
  484. * Return 0 on success, -1 otherwise.
  485. */
  486. static int
  487. set_cluster(fsdata *mydata, __u32 clustnum, __u8 *buffer,
  488. unsigned long size)
  489. {
  490. int idx = 0;
  491. __u32 startsect;
  492. if (clustnum > 0)
  493. startsect = mydata->data_begin +
  494. clustnum * mydata->clust_size;
  495. else
  496. startsect = mydata->rootdir_sect;
  497. debug("clustnum: %d, startsect: %d\n", clustnum, startsect);
  498. if (disk_write(startsect, size / mydata->sect_size, buffer) < 0) {
  499. debug("Error writing data\n");
  500. return -1;
  501. }
  502. if (size % mydata->sect_size) {
  503. __u8 tmpbuf[mydata->sect_size];
  504. idx = size / mydata->sect_size;
  505. buffer += idx * mydata->sect_size;
  506. memcpy(tmpbuf, buffer, size % mydata->sect_size);
  507. if (disk_write(startsect + idx, 1, tmpbuf) < 0) {
  508. debug("Error writing data\n");
  509. return -1;
  510. }
  511. return 0;
  512. }
  513. return 0;
  514. }
  515. /*
  516. * Find the first empty cluster
  517. */
  518. static int find_empty_cluster(fsdata *mydata)
  519. {
  520. __u32 fat_val, entry = 3;
  521. while (1) {
  522. fat_val = get_fatent_value(mydata, entry);
  523. if (fat_val == 0)
  524. break;
  525. entry++;
  526. }
  527. return entry;
  528. }
  529. /*
  530. * Write directory entries in 'get_dentfromdir_block' to block device
  531. */
  532. static void flush_dir_table(fsdata *mydata, dir_entry **dentptr)
  533. {
  534. int dir_newclust = 0;
  535. if (set_cluster(mydata, dir_curclust,
  536. get_dentfromdir_block,
  537. mydata->clust_size * mydata->sect_size) != 0) {
  538. printf("error: wrinting directory entry\n");
  539. return;
  540. }
  541. dir_newclust = find_empty_cluster(mydata);
  542. set_fatent_value(mydata, dir_curclust, dir_newclust);
  543. if (mydata->fatsize == 32)
  544. set_fatent_value(mydata, dir_newclust, 0xffffff8);
  545. else if (mydata->fatsize == 16)
  546. set_fatent_value(mydata, dir_newclust, 0xfff8);
  547. dir_curclust = dir_newclust;
  548. if (flush_fat_buffer(mydata) < 0)
  549. return;
  550. memset(get_dentfromdir_block, 0x00,
  551. mydata->clust_size * mydata->sect_size);
  552. *dentptr = (dir_entry *) get_dentfromdir_block;
  553. }
  554. /*
  555. * Set empty cluster from 'entry' to the end of a file
  556. */
  557. static int clear_fatent(fsdata *mydata, __u32 entry)
  558. {
  559. __u32 fat_val;
  560. while (1) {
  561. fat_val = get_fatent_value(mydata, entry);
  562. if (fat_val != 0)
  563. set_fatent_value(mydata, entry, 0);
  564. else
  565. break;
  566. if (fat_val == 0xfffffff || fat_val == 0xffff)
  567. break;
  568. entry = fat_val;
  569. }
  570. /* Flush fat buffer */
  571. if (flush_fat_buffer(mydata) < 0)
  572. return -1;
  573. return 0;
  574. }
  575. /*
  576. * Write at most 'maxsize' bytes from 'buffer' into
  577. * the file associated with 'dentptr'
  578. * Return the number of bytes read or -1 on fatal errors.
  579. */
  580. static int
  581. set_contents(fsdata *mydata, dir_entry *dentptr, __u8 *buffer,
  582. unsigned long maxsize)
  583. {
  584. unsigned long filesize = FAT2CPU32(dentptr->size), gotsize = 0;
  585. unsigned int bytesperclust = mydata->clust_size * mydata->sect_size;
  586. __u32 curclust = START(dentptr);
  587. __u32 endclust = 0, newclust = 0;
  588. unsigned long actsize;
  589. debug("Filesize: %ld bytes\n", filesize);
  590. if (maxsize > 0 && filesize > maxsize)
  591. filesize = maxsize;
  592. debug("%ld bytes\n", filesize);
  593. actsize = bytesperclust;
  594. endclust = curclust;
  595. do {
  596. /* search for consecutive clusters */
  597. while (actsize < filesize) {
  598. newclust = determine_fatent(mydata, endclust);
  599. if ((newclust - 1) != endclust)
  600. goto getit;
  601. if (CHECK_CLUST(newclust, mydata->fatsize)) {
  602. debug("curclust: 0x%x\n", newclust);
  603. debug("Invalid FAT entry\n");
  604. return gotsize;
  605. }
  606. endclust = newclust;
  607. actsize += bytesperclust;
  608. }
  609. /* actsize >= file size */
  610. actsize -= bytesperclust;
  611. /* set remaining clusters */
  612. if (set_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
  613. debug("error: writing cluster\n");
  614. return -1;
  615. }
  616. /* set remaining bytes */
  617. gotsize += (int)actsize;
  618. filesize -= actsize;
  619. buffer += actsize;
  620. actsize = filesize;
  621. if (set_cluster(mydata, endclust, buffer, (int)actsize) != 0) {
  622. debug("error: writing cluster\n");
  623. return -1;
  624. }
  625. gotsize += actsize;
  626. /* Mark end of file in FAT */
  627. if (mydata->fatsize == 16)
  628. newclust = 0xffff;
  629. else if (mydata->fatsize == 32)
  630. newclust = 0xfffffff;
  631. set_fatent_value(mydata, endclust, newclust);
  632. return gotsize;
  633. getit:
  634. if (set_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
  635. debug("error: writing cluster\n");
  636. return -1;
  637. }
  638. gotsize += (int)actsize;
  639. filesize -= actsize;
  640. buffer += actsize;
  641. if (CHECK_CLUST(curclust, mydata->fatsize)) {
  642. debug("curclust: 0x%x\n", curclust);
  643. debug("Invalid FAT entry\n");
  644. return gotsize;
  645. }
  646. actsize = bytesperclust;
  647. curclust = endclust = newclust;
  648. } while (1);
  649. }
  650. /*
  651. * Fill dir_entry
  652. */
  653. static void fill_dentry(fsdata *mydata, dir_entry *dentptr,
  654. const char *filename, __u32 start_cluster, __u32 size, __u8 attr)
  655. {
  656. if (mydata->fatsize == 32)
  657. dentptr->starthi =
  658. cpu_to_le16((start_cluster & 0xffff0000) >> 16);
  659. dentptr->start = cpu_to_le16(start_cluster & 0xffff);
  660. dentptr->size = cpu_to_le32(size);
  661. dentptr->attr = attr;
  662. set_name(dentptr, filename);
  663. }
  664. /*
  665. * Check whether adding a file makes the file system to
  666. * exceed the size of the block device
  667. * Return -1 when overflow occurs, otherwise return 0
  668. */
  669. static int check_overflow(fsdata *mydata, __u32 clustnum, unsigned long size)
  670. {
  671. __u32 startsect, sect_num;
  672. if (clustnum > 0) {
  673. startsect = mydata->data_begin +
  674. clustnum * mydata->clust_size;
  675. } else {
  676. startsect = mydata->rootdir_sect;
  677. }
  678. sect_num = size / mydata->sect_size;
  679. if (size % mydata->sect_size)
  680. sect_num++;
  681. if (startsect + sect_num > cur_part_info.start + total_sector)
  682. return -1;
  683. return 0;
  684. }
  685. /*
  686. * Check if adding several entries exceed one cluster boundary
  687. */
  688. static int is_next_clust(fsdata *mydata, dir_entry *dentptr)
  689. {
  690. int cur_position;
  691. cur_position = (__u8 *)dentptr - get_dentfromdir_block;
  692. if (cur_position >= mydata->clust_size * mydata->sect_size)
  693. return 1;
  694. else
  695. return 0;
  696. }
  697. static dir_entry *empty_dentptr;
  698. /*
  699. * Find a directory entry based on filename or start cluster number
  700. * If the directory entry is not found,
  701. * the new position for writing a directory entry will be returned
  702. */
  703. static dir_entry *find_directory_entry(fsdata *mydata, int startsect,
  704. char *filename, dir_entry *retdent, __u32 start)
  705. {
  706. __u32 curclust = (startsect - mydata->data_begin) / mydata->clust_size;
  707. debug("get_dentfromdir: %s\n", filename);
  708. while (1) {
  709. dir_entry *dentptr;
  710. int i;
  711. if (get_cluster(mydata, curclust, get_dentfromdir_block,
  712. mydata->clust_size * mydata->sect_size) != 0) {
  713. printf("Error: reading directory block\n");
  714. return NULL;
  715. }
  716. dentptr = (dir_entry *)get_dentfromdir_block;
  717. dir_curclust = curclust;
  718. for (i = 0; i < DIRENTSPERCLUST; i++) {
  719. char s_name[14], l_name[VFAT_MAXLEN_BYTES];
  720. l_name[0] = '\0';
  721. if (dentptr->name[0] == DELETED_FLAG) {
  722. dentptr++;
  723. if (is_next_clust(mydata, dentptr))
  724. break;
  725. continue;
  726. }
  727. if ((dentptr->attr & ATTR_VOLUME)) {
  728. if (vfat_enabled &&
  729. (dentptr->attr & ATTR_VFAT) &&
  730. (dentptr->name[0] & LAST_LONG_ENTRY_MASK)) {
  731. get_long_file_name(mydata, curclust,
  732. get_dentfromdir_block,
  733. &dentptr, l_name);
  734. debug("vfatname: |%s|\n", l_name);
  735. } else {
  736. /* Volume label or VFAT entry */
  737. dentptr++;
  738. if (is_next_clust(mydata, dentptr))
  739. break;
  740. continue;
  741. }
  742. }
  743. if (dentptr->name[0] == 0) {
  744. debug("Dentname == NULL - %d\n", i);
  745. empty_dentptr = dentptr;
  746. return NULL;
  747. }
  748. get_name(dentptr, s_name);
  749. if (strcmp(filename, s_name)
  750. && strcmp(filename, l_name)) {
  751. debug("Mismatch: |%s|%s|\n",
  752. s_name, l_name);
  753. dentptr++;
  754. if (is_next_clust(mydata, dentptr))
  755. break;
  756. continue;
  757. }
  758. memcpy(retdent, dentptr, sizeof(dir_entry));
  759. debug("DentName: %s", s_name);
  760. debug(", start: 0x%x", START(dentptr));
  761. debug(", size: 0x%x %s\n",
  762. FAT2CPU32(dentptr->size),
  763. (dentptr->attr & ATTR_DIR) ?
  764. "(DIR)" : "");
  765. return dentptr;
  766. }
  767. curclust = get_fatent_value(mydata, dir_curclust);
  768. if ((curclust >= 0xffffff8) || (curclust >= 0xfff8)) {
  769. empty_dentptr = dentptr;
  770. return NULL;
  771. }
  772. if (CHECK_CLUST(curclust, mydata->fatsize)) {
  773. debug("curclust: 0x%x\n", curclust);
  774. debug("Invalid FAT entry\n");
  775. return NULL;
  776. }
  777. }
  778. return NULL;
  779. }
  780. static int do_fat_write(const char *filename, void *buffer,
  781. unsigned long size)
  782. {
  783. dir_entry *dentptr, *retdent;
  784. __u32 startsect;
  785. __u32 start_cluster;
  786. boot_sector bs;
  787. volume_info volinfo;
  788. fsdata datablock;
  789. fsdata *mydata = &datablock;
  790. int cursect;
  791. int ret = -1, name_len;
  792. char l_filename[VFAT_MAXLEN_BYTES];
  793. int write_size = size;
  794. dir_curclust = 0;
  795. if (read_bootsectandvi(&bs, &volinfo, &mydata->fatsize)) {
  796. debug("error: reading boot sector\n");
  797. return -1;
  798. }
  799. total_sector = bs.total_sect;
  800. if (total_sector == 0)
  801. total_sector = cur_part_info.size;
  802. if (mydata->fatsize == 32)
  803. mydata->fatlength = bs.fat32_length;
  804. else
  805. mydata->fatlength = bs.fat_length;
  806. mydata->fat_sect = bs.reserved;
  807. cursect = mydata->rootdir_sect
  808. = mydata->fat_sect + mydata->fatlength * bs.fats;
  809. num_of_fats = bs.fats;
  810. mydata->sect_size = (bs.sector_size[1] << 8) + bs.sector_size[0];
  811. mydata->clust_size = bs.cluster_size;
  812. if (mydata->fatsize == 32) {
  813. mydata->data_begin = mydata->rootdir_sect -
  814. (mydata->clust_size * 2);
  815. } else {
  816. int rootdir_size;
  817. rootdir_size = ((bs.dir_entries[1] * (int)256 +
  818. bs.dir_entries[0]) *
  819. sizeof(dir_entry)) /
  820. mydata->sect_size;
  821. mydata->data_begin = mydata->rootdir_sect +
  822. rootdir_size -
  823. (mydata->clust_size * 2);
  824. }
  825. mydata->fatbufnum = -1;
  826. mydata->fatbuf = malloc(FATBUFSIZE);
  827. if (mydata->fatbuf == NULL) {
  828. debug("Error: allocating memory\n");
  829. return -1;
  830. }
  831. if (disk_read(cursect,
  832. (mydata->fatsize == 32) ?
  833. (mydata->clust_size) :
  834. PREFETCH_BLOCKS, do_fat_read_at_block) < 0) {
  835. debug("Error: reading rootdir block\n");
  836. goto exit;
  837. }
  838. dentptr = (dir_entry *) do_fat_read_at_block;
  839. name_len = strlen(filename);
  840. if (name_len >= VFAT_MAXLEN_BYTES)
  841. name_len = VFAT_MAXLEN_BYTES - 1;
  842. memcpy(l_filename, filename, name_len);
  843. l_filename[name_len] = 0; /* terminate the string */
  844. downcase(l_filename);
  845. startsect = mydata->rootdir_sect;
  846. retdent = find_directory_entry(mydata, startsect,
  847. l_filename, dentptr, 0);
  848. if (retdent) {
  849. /* Update file size and start_cluster in a directory entry */
  850. retdent->size = cpu_to_le32(size);
  851. start_cluster = FAT2CPU16(retdent->start);
  852. if (mydata->fatsize == 32)
  853. start_cluster |=
  854. (FAT2CPU16(retdent->starthi) << 16);
  855. ret = check_overflow(mydata, start_cluster, size);
  856. if (ret) {
  857. printf("Error: %ld overflow\n", size);
  858. goto exit;
  859. }
  860. ret = clear_fatent(mydata, start_cluster);
  861. if (ret) {
  862. printf("Error: clearing FAT entries\n");
  863. goto exit;
  864. }
  865. ret = set_contents(mydata, retdent, buffer, size);
  866. if (ret < 0) {
  867. printf("Error: writing contents\n");
  868. goto exit;
  869. }
  870. write_size = ret;
  871. debug("attempt to write 0x%x bytes\n", write_size);
  872. /* Flush fat buffer */
  873. ret = flush_fat_buffer(mydata);
  874. if (ret) {
  875. printf("Error: flush fat buffer\n");
  876. goto exit;
  877. }
  878. /* Write directory table to device */
  879. ret = set_cluster(mydata, dir_curclust,
  880. get_dentfromdir_block,
  881. mydata->clust_size * mydata->sect_size);
  882. if (ret) {
  883. printf("Error: writing directory entry\n");
  884. goto exit;
  885. }
  886. } else {
  887. /* Set short name to set alias checksum field in dir_slot */
  888. set_name(empty_dentptr, filename);
  889. fill_dir_slot(mydata, &empty_dentptr, filename);
  890. ret = start_cluster = find_empty_cluster(mydata);
  891. if (ret < 0) {
  892. printf("Error: finding empty cluster\n");
  893. goto exit;
  894. }
  895. ret = check_overflow(mydata, start_cluster, size);
  896. if (ret) {
  897. printf("Error: %ld overflow\n", size);
  898. goto exit;
  899. }
  900. /* Set attribute as archieve for regular file */
  901. fill_dentry(mydata, empty_dentptr, filename,
  902. start_cluster, size, 0x20);
  903. ret = set_contents(mydata, empty_dentptr, buffer, size);
  904. if (ret < 0) {
  905. printf("Error: writing contents\n");
  906. goto exit;
  907. }
  908. write_size = ret;
  909. debug("attempt to write 0x%x bytes\n", write_size);
  910. /* Flush fat buffer */
  911. ret = flush_fat_buffer(mydata);
  912. if (ret) {
  913. printf("Error: flush fat buffer\n");
  914. goto exit;
  915. }
  916. /* Write directory table to device */
  917. ret = set_cluster(mydata, dir_curclust,
  918. get_dentfromdir_block,
  919. mydata->clust_size * mydata->sect_size);
  920. if (ret) {
  921. printf("Error: writing directory entry\n");
  922. goto exit;
  923. }
  924. }
  925. exit:
  926. free(mydata->fatbuf);
  927. return ret < 0 ? ret : write_size;
  928. }
  929. int file_fat_write(const char *filename, void *buffer, unsigned long maxsize)
  930. {
  931. printf("writing %s\n", filename);
  932. return do_fat_write(filename, buffer, maxsize);
  933. }