fat_write.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * fat_write.c
  4. *
  5. * R/W (V)FAT 12/16/32 filesystem implementation by Donggeun Kim
  6. */
  7. #include <common.h>
  8. #include <command.h>
  9. #include <config.h>
  10. #include <fat.h>
  11. #include <asm/byteorder.h>
  12. #include <part.h>
  13. #include <linux/ctype.h>
  14. #include <div64.h>
  15. #include <linux/math64.h>
  16. #include "fat.c"
  17. static void uppercase(char *str, int len)
  18. {
  19. int i;
  20. for (i = 0; i < len; i++) {
  21. *str = toupper(*str);
  22. str++;
  23. }
  24. }
  25. static int total_sector;
  26. static int disk_write(__u32 block, __u32 nr_blocks, void *buf)
  27. {
  28. ulong ret;
  29. if (!cur_dev)
  30. return -1;
  31. if (cur_part_info.start + block + nr_blocks >
  32. cur_part_info.start + total_sector) {
  33. printf("error: overflow occurs\n");
  34. return -1;
  35. }
  36. ret = blk_dwrite(cur_dev, cur_part_info.start + block, nr_blocks, buf);
  37. if (nr_blocks && ret == 0)
  38. return -1;
  39. return ret;
  40. }
  41. /*
  42. * Set short name in directory entry
  43. */
  44. static void set_name(dir_entry *dirent, const char *filename)
  45. {
  46. char s_name[VFAT_MAXLEN_BYTES];
  47. char *period;
  48. int period_location, len, i, ext_num;
  49. if (filename == NULL)
  50. return;
  51. len = strlen(filename);
  52. if (len == 0)
  53. return;
  54. strcpy(s_name, filename);
  55. uppercase(s_name, len);
  56. period = strchr(s_name, '.');
  57. if (period == NULL) {
  58. period_location = len;
  59. ext_num = 0;
  60. } else {
  61. period_location = period - s_name;
  62. ext_num = len - period_location - 1;
  63. }
  64. /* Pad spaces when the length of file name is shorter than eight */
  65. if (period_location < 8) {
  66. memcpy(dirent->name, s_name, period_location);
  67. for (i = period_location; i < 8; i++)
  68. dirent->name[i] = ' ';
  69. } else if (period_location == 8) {
  70. memcpy(dirent->name, s_name, period_location);
  71. } else {
  72. memcpy(dirent->name, s_name, 6);
  73. dirent->name[6] = '~';
  74. dirent->name[7] = '1';
  75. }
  76. if (ext_num < 3) {
  77. memcpy(dirent->ext, s_name + period_location + 1, ext_num);
  78. for (i = ext_num; i < 3; i++)
  79. dirent->ext[i] = ' ';
  80. } else
  81. memcpy(dirent->ext, s_name + period_location + 1, 3);
  82. debug("name : %s\n", dirent->name);
  83. debug("ext : %s\n", dirent->ext);
  84. }
  85. /*
  86. * Write fat buffer into block device
  87. */
  88. static int flush_dirty_fat_buffer(fsdata *mydata)
  89. {
  90. int getsize = FATBUFBLOCKS;
  91. __u32 fatlength = mydata->fatlength;
  92. __u8 *bufptr = mydata->fatbuf;
  93. __u32 startblock = mydata->fatbufnum * FATBUFBLOCKS;
  94. debug("debug: evicting %d, dirty: %d\n", mydata->fatbufnum,
  95. (int)mydata->fat_dirty);
  96. if ((!mydata->fat_dirty) || (mydata->fatbufnum == -1))
  97. return 0;
  98. /* Cap length if fatlength is not a multiple of FATBUFBLOCKS */
  99. if (startblock + getsize > fatlength)
  100. getsize = fatlength - startblock;
  101. startblock += mydata->fat_sect;
  102. /* Write FAT buf */
  103. if (disk_write(startblock, getsize, bufptr) < 0) {
  104. debug("error: writing FAT blocks\n");
  105. return -1;
  106. }
  107. if (mydata->fats == 2) {
  108. /* Update corresponding second FAT blocks */
  109. startblock += mydata->fatlength;
  110. if (disk_write(startblock, getsize, bufptr) < 0) {
  111. debug("error: writing second FAT blocks\n");
  112. return -1;
  113. }
  114. }
  115. mydata->fat_dirty = 0;
  116. return 0;
  117. }
  118. /*
  119. * Set the file name information from 'name' into 'slotptr',
  120. */
  121. static int str2slot(dir_slot *slotptr, const char *name, int *idx)
  122. {
  123. int j, end_idx = 0;
  124. for (j = 0; j <= 8; j += 2) {
  125. if (name[*idx] == 0x00) {
  126. slotptr->name0_4[j] = 0;
  127. slotptr->name0_4[j + 1] = 0;
  128. end_idx++;
  129. goto name0_4;
  130. }
  131. slotptr->name0_4[j] = name[*idx];
  132. (*idx)++;
  133. end_idx++;
  134. }
  135. for (j = 0; j <= 10; j += 2) {
  136. if (name[*idx] == 0x00) {
  137. slotptr->name5_10[j] = 0;
  138. slotptr->name5_10[j + 1] = 0;
  139. end_idx++;
  140. goto name5_10;
  141. }
  142. slotptr->name5_10[j] = name[*idx];
  143. (*idx)++;
  144. end_idx++;
  145. }
  146. for (j = 0; j <= 2; j += 2) {
  147. if (name[*idx] == 0x00) {
  148. slotptr->name11_12[j] = 0;
  149. slotptr->name11_12[j + 1] = 0;
  150. end_idx++;
  151. goto name11_12;
  152. }
  153. slotptr->name11_12[j] = name[*idx];
  154. (*idx)++;
  155. end_idx++;
  156. }
  157. if (name[*idx] == 0x00)
  158. return 1;
  159. return 0;
  160. /* Not used characters are filled with 0xff 0xff */
  161. name0_4:
  162. for (; end_idx < 5; end_idx++) {
  163. slotptr->name0_4[end_idx * 2] = 0xff;
  164. slotptr->name0_4[end_idx * 2 + 1] = 0xff;
  165. }
  166. end_idx = 5;
  167. name5_10:
  168. end_idx -= 5;
  169. for (; end_idx < 6; end_idx++) {
  170. slotptr->name5_10[end_idx * 2] = 0xff;
  171. slotptr->name5_10[end_idx * 2 + 1] = 0xff;
  172. }
  173. end_idx = 11;
  174. name11_12:
  175. end_idx -= 11;
  176. for (; end_idx < 2; end_idx++) {
  177. slotptr->name11_12[end_idx * 2] = 0xff;
  178. slotptr->name11_12[end_idx * 2 + 1] = 0xff;
  179. }
  180. return 1;
  181. }
  182. static int new_dir_table(fat_itr *itr);
  183. static int flush_dir(fat_itr *itr);
  184. /*
  185. * Fill dir_slot entries with appropriate name, id, and attr
  186. * 'itr' will point to a next entry
  187. */
  188. static int
  189. fill_dir_slot(fat_itr *itr, const char *l_name)
  190. {
  191. __u8 temp_dir_slot_buffer[MAX_LFN_SLOT * sizeof(dir_slot)];
  192. dir_slot *slotptr = (dir_slot *)temp_dir_slot_buffer;
  193. __u8 counter = 0, checksum;
  194. int idx = 0, ret;
  195. /* Get short file name checksum value */
  196. checksum = mkcksum(itr->dent->name, itr->dent->ext);
  197. do {
  198. memset(slotptr, 0x00, sizeof(dir_slot));
  199. ret = str2slot(slotptr, l_name, &idx);
  200. slotptr->id = ++counter;
  201. slotptr->attr = ATTR_VFAT;
  202. slotptr->alias_checksum = checksum;
  203. slotptr++;
  204. } while (ret == 0);
  205. slotptr--;
  206. slotptr->id |= LAST_LONG_ENTRY_MASK;
  207. while (counter >= 1) {
  208. memcpy(itr->dent, slotptr, sizeof(dir_slot));
  209. slotptr--;
  210. counter--;
  211. if (itr->remaining == 0)
  212. flush_dir(itr);
  213. /* allocate a cluster for more entries */
  214. if (!fat_itr_next(itr))
  215. if (!itr->dent &&
  216. (!itr->is_root || itr->fsdata->fatsize == 32) &&
  217. new_dir_table(itr))
  218. return -1;
  219. }
  220. return 0;
  221. }
  222. /*
  223. * Set the entry at index 'entry' in a FAT (12/16/32) table.
  224. */
  225. static int set_fatent_value(fsdata *mydata, __u32 entry, __u32 entry_value)
  226. {
  227. __u32 bufnum, offset, off16;
  228. __u16 val1, val2;
  229. switch (mydata->fatsize) {
  230. case 32:
  231. bufnum = entry / FAT32BUFSIZE;
  232. offset = entry - bufnum * FAT32BUFSIZE;
  233. break;
  234. case 16:
  235. bufnum = entry / FAT16BUFSIZE;
  236. offset = entry - bufnum * FAT16BUFSIZE;
  237. break;
  238. case 12:
  239. bufnum = entry / FAT12BUFSIZE;
  240. offset = entry - bufnum * FAT12BUFSIZE;
  241. break;
  242. default:
  243. /* Unsupported FAT size */
  244. return -1;
  245. }
  246. /* Read a new block of FAT entries into the cache. */
  247. if (bufnum != mydata->fatbufnum) {
  248. int getsize = FATBUFBLOCKS;
  249. __u8 *bufptr = mydata->fatbuf;
  250. __u32 fatlength = mydata->fatlength;
  251. __u32 startblock = bufnum * FATBUFBLOCKS;
  252. /* Cap length if fatlength is not a multiple of FATBUFBLOCKS */
  253. if (startblock + getsize > fatlength)
  254. getsize = fatlength - startblock;
  255. if (flush_dirty_fat_buffer(mydata) < 0)
  256. return -1;
  257. startblock += mydata->fat_sect;
  258. if (disk_read(startblock, getsize, bufptr) < 0) {
  259. debug("Error reading FAT blocks\n");
  260. return -1;
  261. }
  262. mydata->fatbufnum = bufnum;
  263. }
  264. /* Mark as dirty */
  265. mydata->fat_dirty = 1;
  266. /* Set the actual entry */
  267. switch (mydata->fatsize) {
  268. case 32:
  269. ((__u32 *) mydata->fatbuf)[offset] = cpu_to_le32(entry_value);
  270. break;
  271. case 16:
  272. ((__u16 *) mydata->fatbuf)[offset] = cpu_to_le16(entry_value);
  273. break;
  274. case 12:
  275. off16 = (offset * 3) / 4;
  276. switch (offset & 0x3) {
  277. case 0:
  278. val1 = cpu_to_le16(entry_value) & 0xfff;
  279. ((__u16 *)mydata->fatbuf)[off16] &= ~0xfff;
  280. ((__u16 *)mydata->fatbuf)[off16] |= val1;
  281. break;
  282. case 1:
  283. val1 = cpu_to_le16(entry_value) & 0xf;
  284. val2 = (cpu_to_le16(entry_value) >> 4) & 0xff;
  285. ((__u16 *)mydata->fatbuf)[off16] &= ~0xf000;
  286. ((__u16 *)mydata->fatbuf)[off16] |= (val1 << 12);
  287. ((__u16 *)mydata->fatbuf)[off16 + 1] &= ~0xff;
  288. ((__u16 *)mydata->fatbuf)[off16 + 1] |= val2;
  289. break;
  290. case 2:
  291. val1 = cpu_to_le16(entry_value) & 0xff;
  292. val2 = (cpu_to_le16(entry_value) >> 8) & 0xf;
  293. ((__u16 *)mydata->fatbuf)[off16] &= ~0xff00;
  294. ((__u16 *)mydata->fatbuf)[off16] |= (val1 << 8);
  295. ((__u16 *)mydata->fatbuf)[off16 + 1] &= ~0xf;
  296. ((__u16 *)mydata->fatbuf)[off16 + 1] |= val2;
  297. break;
  298. case 3:
  299. val1 = cpu_to_le16(entry_value) & 0xfff;
  300. ((__u16 *)mydata->fatbuf)[off16] &= ~0xfff0;
  301. ((__u16 *)mydata->fatbuf)[off16] |= (val1 << 4);
  302. break;
  303. default:
  304. break;
  305. }
  306. break;
  307. default:
  308. return -1;
  309. }
  310. return 0;
  311. }
  312. /*
  313. * Determine the next free cluster after 'entry' in a FAT (12/16/32) table
  314. * and link it to 'entry'. EOC marker is not set on returned entry.
  315. */
  316. static __u32 determine_fatent(fsdata *mydata, __u32 entry)
  317. {
  318. __u32 next_fat, next_entry = entry + 1;
  319. while (1) {
  320. next_fat = get_fatent(mydata, next_entry);
  321. if (next_fat == 0) {
  322. /* found free entry, link to entry */
  323. set_fatent_value(mydata, entry, next_entry);
  324. break;
  325. }
  326. next_entry++;
  327. }
  328. debug("FAT%d: entry: %08x, entry_value: %04x\n",
  329. mydata->fatsize, entry, next_entry);
  330. return next_entry;
  331. }
  332. /**
  333. * set_sectors() - write data to sectors
  334. *
  335. * Write 'size' bytes from 'buffer' into the specified sector.
  336. *
  337. * @mydata: data to be written
  338. * @startsect: sector to be written to
  339. * @buffer: data to be written
  340. * @size: bytes to be written (but not more than the size of a cluster)
  341. * Return: 0 on success, -1 otherwise
  342. */
  343. static int
  344. set_sectors(fsdata *mydata, u32 startsect, u8 *buffer, u32 size)
  345. {
  346. u32 nsects = 0;
  347. int ret;
  348. debug("startsect: %d\n", startsect);
  349. if ((unsigned long)buffer & (ARCH_DMA_MINALIGN - 1)) {
  350. ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
  351. debug("FAT: Misaligned buffer address (%p)\n", buffer);
  352. while (size >= mydata->sect_size) {
  353. memcpy(tmpbuf, buffer, mydata->sect_size);
  354. ret = disk_write(startsect++, 1, tmpbuf);
  355. if (ret != 1) {
  356. debug("Error writing data (got %d)\n", ret);
  357. return -1;
  358. }
  359. buffer += mydata->sect_size;
  360. size -= mydata->sect_size;
  361. }
  362. } else if (size >= mydata->sect_size) {
  363. nsects = size / mydata->sect_size;
  364. ret = disk_write(startsect, nsects, buffer);
  365. if (ret != nsects) {
  366. debug("Error writing data (got %d)\n", ret);
  367. return -1;
  368. }
  369. startsect += nsects;
  370. buffer += nsects * mydata->sect_size;
  371. size -= nsects * mydata->sect_size;
  372. }
  373. if (size) {
  374. ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
  375. /* Do not leak content of stack */
  376. memset(tmpbuf, 0, mydata->sect_size);
  377. memcpy(tmpbuf, buffer, size);
  378. ret = disk_write(startsect, 1, tmpbuf);
  379. if (ret != 1) {
  380. debug("Error writing data (got %d)\n", ret);
  381. return -1;
  382. }
  383. }
  384. return 0;
  385. }
  386. /**
  387. * set_cluster() - write data to cluster
  388. *
  389. * Write 'size' bytes from 'buffer' into the specified cluster.
  390. *
  391. * @mydata: data to be written
  392. * @clustnum: cluster to be written to
  393. * @buffer: data to be written
  394. * @size: bytes to be written (but not more than the size of a cluster)
  395. * Return: 0 on success, -1 otherwise
  396. */
  397. static int
  398. set_cluster(fsdata *mydata, u32 clustnum, u8 *buffer, u32 size)
  399. {
  400. return set_sectors(mydata, clust_to_sect(mydata, clustnum),
  401. buffer, size);
  402. }
  403. static int
  404. flush_dir(fat_itr *itr)
  405. {
  406. fsdata *mydata = itr->fsdata;
  407. u32 startsect, sect_offset, nsects;
  408. if (!itr->is_root || mydata->fatsize == 32)
  409. return set_cluster(mydata, itr->clust, itr->block,
  410. mydata->clust_size * mydata->sect_size);
  411. sect_offset = itr->clust * mydata->clust_size;
  412. startsect = mydata->rootdir_sect + sect_offset;
  413. /* do not write past the end of rootdir */
  414. nsects = min_t(u32, mydata->clust_size,
  415. mydata->rootdir_size - sect_offset);
  416. return set_sectors(mydata, startsect, itr->block,
  417. nsects * mydata->sect_size);
  418. }
  419. static __u8 tmpbuf_cluster[MAX_CLUSTSIZE] __aligned(ARCH_DMA_MINALIGN);
  420. /*
  421. * Read and modify data on existing and consecutive cluster blocks
  422. */
  423. static int
  424. get_set_cluster(fsdata *mydata, __u32 clustnum, loff_t pos, __u8 *buffer,
  425. loff_t size, loff_t *gotsize)
  426. {
  427. unsigned int bytesperclust = mydata->clust_size * mydata->sect_size;
  428. __u32 startsect;
  429. loff_t wsize;
  430. int clustcount, i, ret;
  431. *gotsize = 0;
  432. if (!size)
  433. return 0;
  434. assert(pos < bytesperclust);
  435. startsect = clust_to_sect(mydata, clustnum);
  436. debug("clustnum: %d, startsect: %d, pos: %lld\n",
  437. clustnum, startsect, pos);
  438. /* partial write at beginning */
  439. if (pos) {
  440. wsize = min(bytesperclust - pos, size);
  441. ret = disk_read(startsect, mydata->clust_size, tmpbuf_cluster);
  442. if (ret != mydata->clust_size) {
  443. debug("Error reading data (got %d)\n", ret);
  444. return -1;
  445. }
  446. memcpy(tmpbuf_cluster + pos, buffer, wsize);
  447. ret = disk_write(startsect, mydata->clust_size, tmpbuf_cluster);
  448. if (ret != mydata->clust_size) {
  449. debug("Error writing data (got %d)\n", ret);
  450. return -1;
  451. }
  452. size -= wsize;
  453. buffer += wsize;
  454. *gotsize += wsize;
  455. startsect += mydata->clust_size;
  456. if (!size)
  457. return 0;
  458. }
  459. /* full-cluster write */
  460. if (size >= bytesperclust) {
  461. clustcount = lldiv(size, bytesperclust);
  462. if (!((unsigned long)buffer & (ARCH_DMA_MINALIGN - 1))) {
  463. wsize = clustcount * bytesperclust;
  464. ret = disk_write(startsect,
  465. clustcount * mydata->clust_size,
  466. buffer);
  467. if (ret != clustcount * mydata->clust_size) {
  468. debug("Error writing data (got %d)\n", ret);
  469. return -1;
  470. }
  471. size -= wsize;
  472. buffer += wsize;
  473. *gotsize += wsize;
  474. startsect += clustcount * mydata->clust_size;
  475. } else {
  476. for (i = 0; i < clustcount; i++) {
  477. memcpy(tmpbuf_cluster, buffer, bytesperclust);
  478. ret = disk_write(startsect,
  479. mydata->clust_size,
  480. tmpbuf_cluster);
  481. if (ret != mydata->clust_size) {
  482. debug("Error writing data (got %d)\n",
  483. ret);
  484. return -1;
  485. }
  486. size -= bytesperclust;
  487. buffer += bytesperclust;
  488. *gotsize += bytesperclust;
  489. startsect += mydata->clust_size;
  490. }
  491. }
  492. }
  493. /* partial write at end */
  494. if (size) {
  495. wsize = size;
  496. ret = disk_read(startsect, mydata->clust_size, tmpbuf_cluster);
  497. if (ret != mydata->clust_size) {
  498. debug("Error reading data (got %d)\n", ret);
  499. return -1;
  500. }
  501. memcpy(tmpbuf_cluster, buffer, wsize);
  502. ret = disk_write(startsect, mydata->clust_size, tmpbuf_cluster);
  503. if (ret != mydata->clust_size) {
  504. debug("Error writing data (got %d)\n", ret);
  505. return -1;
  506. }
  507. size -= wsize;
  508. buffer += wsize;
  509. *gotsize += wsize;
  510. }
  511. assert(!size);
  512. return 0;
  513. }
  514. /*
  515. * Find the first empty cluster
  516. */
  517. static int find_empty_cluster(fsdata *mydata)
  518. {
  519. __u32 fat_val, entry = 3;
  520. while (1) {
  521. fat_val = get_fatent(mydata, entry);
  522. if (fat_val == 0)
  523. break;
  524. entry++;
  525. }
  526. return entry;
  527. }
  528. /*
  529. * Allocate a cluster for additional directory entries
  530. */
  531. static int new_dir_table(fat_itr *itr)
  532. {
  533. fsdata *mydata = itr->fsdata;
  534. int dir_newclust = 0;
  535. unsigned int bytesperclust = mydata->clust_size * mydata->sect_size;
  536. dir_newclust = find_empty_cluster(mydata);
  537. set_fatent_value(mydata, itr->clust, dir_newclust);
  538. if (mydata->fatsize == 32)
  539. set_fatent_value(mydata, dir_newclust, 0xffffff8);
  540. else if (mydata->fatsize == 16)
  541. set_fatent_value(mydata, dir_newclust, 0xfff8);
  542. else if (mydata->fatsize == 12)
  543. set_fatent_value(mydata, dir_newclust, 0xff8);
  544. itr->clust = dir_newclust;
  545. itr->next_clust = dir_newclust;
  546. if (flush_dirty_fat_buffer(mydata) < 0)
  547. return -1;
  548. memset(itr->block, 0x00, bytesperclust);
  549. itr->dent = (dir_entry *)itr->block;
  550. itr->last_cluster = 1;
  551. itr->remaining = bytesperclust / sizeof(dir_entry) - 1;
  552. return 0;
  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 (!CHECK_CLUST(entry, mydata->fatsize)) {
  561. fat_val = get_fatent(mydata, entry);
  562. if (fat_val != 0)
  563. set_fatent_value(mydata, entry, 0);
  564. else
  565. break;
  566. entry = fat_val;
  567. }
  568. /* Flush fat buffer */
  569. if (flush_dirty_fat_buffer(mydata) < 0)
  570. return -1;
  571. return 0;
  572. }
  573. /*
  574. * Set start cluster in directory entry
  575. */
  576. static void set_start_cluster(const fsdata *mydata, dir_entry *dentptr,
  577. __u32 start_cluster)
  578. {
  579. if (mydata->fatsize == 32)
  580. dentptr->starthi =
  581. cpu_to_le16((start_cluster & 0xffff0000) >> 16);
  582. dentptr->start = cpu_to_le16(start_cluster & 0xffff);
  583. }
  584. /*
  585. * Check whether adding a file makes the file system to
  586. * exceed the size of the block device
  587. * Return -1 when overflow occurs, otherwise return 0
  588. */
  589. static int check_overflow(fsdata *mydata, __u32 clustnum, loff_t size)
  590. {
  591. __u32 startsect, sect_num, offset;
  592. if (clustnum > 0)
  593. startsect = clust_to_sect(mydata, clustnum);
  594. else
  595. startsect = mydata->rootdir_sect;
  596. sect_num = div_u64_rem(size, mydata->sect_size, &offset);
  597. if (offset != 0)
  598. sect_num++;
  599. if (startsect + sect_num > total_sector)
  600. return -1;
  601. return 0;
  602. }
  603. /*
  604. * Write at most 'maxsize' bytes from 'buffer' into
  605. * the file associated with 'dentptr'
  606. * Update the number of bytes written in *gotsize and return 0
  607. * or return -1 on fatal errors.
  608. */
  609. static int
  610. set_contents(fsdata *mydata, dir_entry *dentptr, loff_t pos, __u8 *buffer,
  611. loff_t maxsize, loff_t *gotsize)
  612. {
  613. unsigned int bytesperclust = mydata->clust_size * mydata->sect_size;
  614. __u32 curclust = START(dentptr);
  615. __u32 endclust = 0, newclust = 0;
  616. u64 cur_pos, filesize;
  617. loff_t offset, actsize, wsize;
  618. *gotsize = 0;
  619. filesize = pos + maxsize;
  620. debug("%llu bytes\n", filesize);
  621. if (!filesize) {
  622. if (!curclust)
  623. return 0;
  624. if (!CHECK_CLUST(curclust, mydata->fatsize) ||
  625. IS_LAST_CLUST(curclust, mydata->fatsize)) {
  626. clear_fatent(mydata, curclust);
  627. set_start_cluster(mydata, dentptr, 0);
  628. return 0;
  629. }
  630. debug("curclust: 0x%x\n", curclust);
  631. debug("Invalid FAT entry\n");
  632. return -1;
  633. }
  634. if (!curclust) {
  635. assert(pos == 0);
  636. goto set_clusters;
  637. }
  638. /* go to cluster at pos */
  639. cur_pos = bytesperclust;
  640. while (1) {
  641. if (pos <= cur_pos)
  642. break;
  643. if (IS_LAST_CLUST(curclust, mydata->fatsize))
  644. break;
  645. newclust = get_fatent(mydata, curclust);
  646. if (!IS_LAST_CLUST(newclust, mydata->fatsize) &&
  647. CHECK_CLUST(newclust, mydata->fatsize)) {
  648. debug("curclust: 0x%x\n", curclust);
  649. debug("Invalid FAT entry\n");
  650. return -1;
  651. }
  652. cur_pos += bytesperclust;
  653. curclust = newclust;
  654. }
  655. if (IS_LAST_CLUST(curclust, mydata->fatsize)) {
  656. assert(pos == cur_pos);
  657. goto set_clusters;
  658. }
  659. assert(pos < cur_pos);
  660. cur_pos -= bytesperclust;
  661. /* overwrite */
  662. assert(IS_LAST_CLUST(curclust, mydata->fatsize) ||
  663. !CHECK_CLUST(curclust, mydata->fatsize));
  664. while (1) {
  665. /* search for allocated consecutive clusters */
  666. actsize = bytesperclust;
  667. endclust = curclust;
  668. while (1) {
  669. if (filesize <= (cur_pos + actsize))
  670. break;
  671. newclust = get_fatent(mydata, endclust);
  672. if (IS_LAST_CLUST(newclust, mydata->fatsize))
  673. break;
  674. if (CHECK_CLUST(newclust, mydata->fatsize)) {
  675. debug("curclust: 0x%x\n", curclust);
  676. debug("Invalid FAT entry\n");
  677. return -1;
  678. }
  679. actsize += bytesperclust;
  680. endclust = newclust;
  681. }
  682. /* overwrite to <curclust..endclust> */
  683. if (pos < cur_pos)
  684. offset = 0;
  685. else
  686. offset = pos - cur_pos;
  687. wsize = min(cur_pos + actsize, filesize) - pos;
  688. if (get_set_cluster(mydata, curclust, offset,
  689. buffer, wsize, &actsize)) {
  690. printf("Error get-and-setting cluster\n");
  691. return -1;
  692. }
  693. buffer += wsize;
  694. *gotsize += wsize;
  695. cur_pos += offset + wsize;
  696. if (filesize <= cur_pos)
  697. break;
  698. /* CHECK: newclust = get_fatent(mydata, endclust); */
  699. if (IS_LAST_CLUST(newclust, mydata->fatsize))
  700. /* no more clusters */
  701. break;
  702. curclust = newclust;
  703. }
  704. if (filesize <= cur_pos) {
  705. /* no more write */
  706. newclust = get_fatent(mydata, endclust);
  707. if (!IS_LAST_CLUST(newclust, mydata->fatsize)) {
  708. /* truncate the rest */
  709. clear_fatent(mydata, newclust);
  710. /* Mark end of file in FAT */
  711. if (mydata->fatsize == 12)
  712. newclust = 0xfff;
  713. else if (mydata->fatsize == 16)
  714. newclust = 0xffff;
  715. else if (mydata->fatsize == 32)
  716. newclust = 0xfffffff;
  717. set_fatent_value(mydata, endclust, newclust);
  718. }
  719. return 0;
  720. }
  721. curclust = endclust;
  722. filesize -= cur_pos;
  723. assert(!do_div(cur_pos, bytesperclust));
  724. set_clusters:
  725. /* allocate and write */
  726. assert(!pos);
  727. /* Assure that curclust is valid */
  728. if (!curclust) {
  729. curclust = find_empty_cluster(mydata);
  730. set_start_cluster(mydata, dentptr, curclust);
  731. } else {
  732. newclust = get_fatent(mydata, curclust);
  733. if (IS_LAST_CLUST(newclust, mydata->fatsize)) {
  734. newclust = determine_fatent(mydata, curclust);
  735. set_fatent_value(mydata, curclust, newclust);
  736. curclust = newclust;
  737. } else {
  738. debug("error: something wrong\n");
  739. return -1;
  740. }
  741. }
  742. /* TODO: already partially written */
  743. if (check_overflow(mydata, curclust, filesize)) {
  744. printf("Error: no space left: %llu\n", filesize);
  745. return -1;
  746. }
  747. actsize = bytesperclust;
  748. endclust = curclust;
  749. do {
  750. /* search for consecutive clusters */
  751. while (actsize < filesize) {
  752. newclust = determine_fatent(mydata, endclust);
  753. if ((newclust - 1) != endclust)
  754. /* write to <curclust..endclust> */
  755. goto getit;
  756. if (CHECK_CLUST(newclust, mydata->fatsize)) {
  757. debug("newclust: 0x%x\n", newclust);
  758. debug("Invalid FAT entry\n");
  759. return 0;
  760. }
  761. endclust = newclust;
  762. actsize += bytesperclust;
  763. }
  764. /* set remaining bytes */
  765. actsize = filesize;
  766. if (set_cluster(mydata, curclust, buffer, (u32)actsize) != 0) {
  767. debug("error: writing cluster\n");
  768. return -1;
  769. }
  770. *gotsize += actsize;
  771. /* Mark end of file in FAT */
  772. if (mydata->fatsize == 12)
  773. newclust = 0xfff;
  774. else if (mydata->fatsize == 16)
  775. newclust = 0xffff;
  776. else if (mydata->fatsize == 32)
  777. newclust = 0xfffffff;
  778. set_fatent_value(mydata, endclust, newclust);
  779. return 0;
  780. getit:
  781. if (set_cluster(mydata, curclust, buffer, (u32)actsize) != 0) {
  782. debug("error: writing cluster\n");
  783. return -1;
  784. }
  785. *gotsize += actsize;
  786. filesize -= actsize;
  787. buffer += actsize;
  788. if (CHECK_CLUST(newclust, mydata->fatsize)) {
  789. debug("newclust: 0x%x\n", newclust);
  790. debug("Invalid FAT entry\n");
  791. return 0;
  792. }
  793. actsize = bytesperclust;
  794. curclust = endclust = newclust;
  795. } while (1);
  796. return 0;
  797. }
  798. /*
  799. * Fill dir_entry
  800. */
  801. static void fill_dentry(fsdata *mydata, dir_entry *dentptr,
  802. const char *filename, __u32 start_cluster, __u32 size, __u8 attr)
  803. {
  804. set_start_cluster(mydata, dentptr, start_cluster);
  805. dentptr->size = cpu_to_le32(size);
  806. dentptr->attr = attr;
  807. set_name(dentptr, filename);
  808. }
  809. /*
  810. * Find a directory entry based on filename or start cluster number
  811. * If the directory entry is not found,
  812. * the new position for writing a directory entry will be returned
  813. */
  814. static dir_entry *find_directory_entry(fat_itr *itr, char *filename)
  815. {
  816. int match = 0;
  817. while (fat_itr_next(itr)) {
  818. /* check both long and short name: */
  819. if (!strcasecmp(filename, itr->name))
  820. match = 1;
  821. else if (itr->name != itr->s_name &&
  822. !strcasecmp(filename, itr->s_name))
  823. match = 1;
  824. if (!match)
  825. continue;
  826. if (itr->dent->name[0] == '\0')
  827. return NULL;
  828. else
  829. return itr->dent;
  830. }
  831. /* allocate a cluster for more entries */
  832. if (!itr->dent &&
  833. (!itr->is_root || itr->fsdata->fatsize == 32) &&
  834. new_dir_table(itr))
  835. /* indicate that allocating dent failed */
  836. itr->dent = NULL;
  837. return NULL;
  838. }
  839. static int split_filename(char *filename, char **dirname, char **basename)
  840. {
  841. char *p, *last_slash, *last_slash_cont;
  842. again:
  843. p = filename;
  844. last_slash = NULL;
  845. last_slash_cont = NULL;
  846. while (*p) {
  847. if (ISDIRDELIM(*p)) {
  848. last_slash = p;
  849. last_slash_cont = p;
  850. /* continuous slashes */
  851. while (ISDIRDELIM(*p))
  852. last_slash_cont = p++;
  853. if (!*p)
  854. break;
  855. }
  856. p++;
  857. }
  858. if (last_slash) {
  859. if (last_slash_cont == (filename + strlen(filename) - 1)) {
  860. /* remove trailing slashes */
  861. *last_slash = '\0';
  862. goto again;
  863. }
  864. if (last_slash == filename) {
  865. /* avoid ""(null) directory */
  866. *dirname = "/";
  867. } else {
  868. *last_slash = '\0';
  869. *dirname = filename;
  870. }
  871. *last_slash_cont = '\0';
  872. *basename = last_slash_cont + 1;
  873. } else {
  874. *dirname = "/"; /* root by default */
  875. *basename = filename;
  876. }
  877. return 0;
  878. }
  879. /**
  880. * normalize_longname() - check long file name and convert to lower case
  881. *
  882. * We assume here that the FAT file system is using an 8bit code page.
  883. * Linux typically uses CP437, EDK2 assumes CP1250.
  884. *
  885. * @l_filename: preallocated buffer receiving the normalized name
  886. * @filename: filename to normalize
  887. * Return: 0 on success, -1 on failure
  888. */
  889. static int normalize_longname(char *l_filename, const char *filename)
  890. {
  891. const char *p, illegal[] = "<>:\"/\\|?*";
  892. if (strlen(filename) >= VFAT_MAXLEN_BYTES)
  893. return -1;
  894. for (p = filename; *p; ++p) {
  895. if ((unsigned char)*p < 0x20)
  896. return -1;
  897. if (strchr(illegal, *p))
  898. return -1;
  899. }
  900. strcpy(l_filename, filename);
  901. downcase(l_filename, VFAT_MAXLEN_BYTES);
  902. return 0;
  903. }
  904. int file_fat_write_at(const char *filename, loff_t pos, void *buffer,
  905. loff_t size, loff_t *actwrite)
  906. {
  907. dir_entry *retdent;
  908. fsdata datablock = { .fatbuf = NULL, };
  909. fsdata *mydata = &datablock;
  910. fat_itr *itr = NULL;
  911. int ret = -1;
  912. char *filename_copy, *parent, *basename;
  913. char l_filename[VFAT_MAXLEN_BYTES];
  914. debug("writing %s\n", filename);
  915. filename_copy = strdup(filename);
  916. if (!filename_copy)
  917. return -ENOMEM;
  918. split_filename(filename_copy, &parent, &basename);
  919. if (!strlen(basename)) {
  920. ret = -EINVAL;
  921. goto exit;
  922. }
  923. filename = basename;
  924. if (normalize_longname(l_filename, filename)) {
  925. printf("FAT: illegal filename (%s)\n", filename);
  926. ret = -EINVAL;
  927. goto exit;
  928. }
  929. itr = malloc_cache_aligned(sizeof(fat_itr));
  930. if (!itr) {
  931. ret = -ENOMEM;
  932. goto exit;
  933. }
  934. ret = fat_itr_root(itr, &datablock);
  935. if (ret)
  936. goto exit;
  937. total_sector = datablock.total_sect;
  938. ret = fat_itr_resolve(itr, parent, TYPE_DIR);
  939. if (ret) {
  940. printf("%s: doesn't exist (%d)\n", parent, ret);
  941. goto exit;
  942. }
  943. retdent = find_directory_entry(itr, l_filename);
  944. if (retdent) {
  945. if (fat_itr_isdir(itr)) {
  946. ret = -EISDIR;
  947. goto exit;
  948. }
  949. /* A file exists */
  950. if (pos == -1)
  951. /* Append to the end */
  952. pos = FAT2CPU32(retdent->size);
  953. if (pos > retdent->size) {
  954. /* No hole allowed */
  955. ret = -EINVAL;
  956. goto exit;
  957. }
  958. /* Update file size in a directory entry */
  959. retdent->size = cpu_to_le32(pos + size);
  960. } else {
  961. /* Create a new file */
  962. if (itr->is_root) {
  963. /* root dir cannot have "." or ".." */
  964. if (!strcmp(l_filename, ".") ||
  965. !strcmp(l_filename, "..")) {
  966. ret = -EINVAL;
  967. goto exit;
  968. }
  969. }
  970. if (!itr->dent) {
  971. printf("Error: allocating new dir entry\n");
  972. ret = -EIO;
  973. goto exit;
  974. }
  975. if (pos) {
  976. /* No hole allowed */
  977. ret = -EINVAL;
  978. goto exit;
  979. }
  980. memset(itr->dent, 0, sizeof(*itr->dent));
  981. /* Calculate checksum for short name */
  982. set_name(itr->dent, filename);
  983. /* Set long name entries */
  984. if (fill_dir_slot(itr, filename)) {
  985. ret = -EIO;
  986. goto exit;
  987. }
  988. /* Set short name entry */
  989. fill_dentry(itr->fsdata, itr->dent, filename, 0, size, 0x20);
  990. retdent = itr->dent;
  991. }
  992. ret = set_contents(mydata, retdent, pos, buffer, size, actwrite);
  993. if (ret < 0) {
  994. printf("Error: writing contents\n");
  995. ret = -EIO;
  996. goto exit;
  997. }
  998. debug("attempt to write 0x%llx bytes\n", *actwrite);
  999. /* Flush fat buffer */
  1000. ret = flush_dirty_fat_buffer(mydata);
  1001. if (ret) {
  1002. printf("Error: flush fat buffer\n");
  1003. ret = -EIO;
  1004. goto exit;
  1005. }
  1006. /* Write directory table to device */
  1007. ret = flush_dir(itr);
  1008. if (ret) {
  1009. printf("Error: writing directory entry\n");
  1010. ret = -EIO;
  1011. }
  1012. exit:
  1013. free(filename_copy);
  1014. free(mydata->fatbuf);
  1015. free(itr);
  1016. return ret;
  1017. }
  1018. int file_fat_write(const char *filename, void *buffer, loff_t offset,
  1019. loff_t maxsize, loff_t *actwrite)
  1020. {
  1021. return file_fat_write_at(filename, offset, buffer, maxsize, actwrite);
  1022. }
  1023. static int fat_dir_entries(fat_itr *itr)
  1024. {
  1025. fat_itr *dirs;
  1026. fsdata fsdata = { .fatbuf = NULL, }, *mydata = &fsdata;
  1027. /* for FATBUFSIZE */
  1028. int count;
  1029. dirs = malloc_cache_aligned(sizeof(fat_itr));
  1030. if (!dirs) {
  1031. debug("Error: allocating memory\n");
  1032. count = -ENOMEM;
  1033. goto exit;
  1034. }
  1035. /* duplicate fsdata */
  1036. fat_itr_child(dirs, itr);
  1037. fsdata = *dirs->fsdata;
  1038. /* allocate local fat buffer */
  1039. fsdata.fatbuf = malloc_cache_aligned(FATBUFSIZE);
  1040. if (!fsdata.fatbuf) {
  1041. debug("Error: allocating memory\n");
  1042. count = -ENOMEM;
  1043. goto exit;
  1044. }
  1045. fsdata.fatbufnum = -1;
  1046. dirs->fsdata = &fsdata;
  1047. for (count = 0; fat_itr_next(dirs); count++)
  1048. ;
  1049. exit:
  1050. free(fsdata.fatbuf);
  1051. free(dirs);
  1052. return count;
  1053. }
  1054. static int delete_dentry(fat_itr *itr)
  1055. {
  1056. fsdata *mydata = itr->fsdata;
  1057. dir_entry *dentptr = itr->dent;
  1058. /* free cluster blocks */
  1059. clear_fatent(mydata, START(dentptr));
  1060. if (flush_dirty_fat_buffer(mydata) < 0) {
  1061. printf("Error: flush fat buffer\n");
  1062. return -EIO;
  1063. }
  1064. /*
  1065. * update a directory entry
  1066. * TODO:
  1067. * - long file name support
  1068. * - find and mark the "new" first invalid entry as name[0]=0x00
  1069. */
  1070. memset(dentptr, 0, sizeof(*dentptr));
  1071. dentptr->name[0] = 0xe5;
  1072. if (flush_dir(itr)) {
  1073. printf("error: writing directory entry\n");
  1074. return -EIO;
  1075. }
  1076. return 0;
  1077. }
  1078. int fat_unlink(const char *filename)
  1079. {
  1080. fsdata fsdata = { .fatbuf = NULL, };
  1081. fat_itr *itr = NULL;
  1082. int n_entries, ret;
  1083. char *filename_copy, *dirname, *basename;
  1084. filename_copy = strdup(filename);
  1085. if (!filename_copy) {
  1086. printf("Error: allocating memory\n");
  1087. ret = -ENOMEM;
  1088. goto exit;
  1089. }
  1090. split_filename(filename_copy, &dirname, &basename);
  1091. if (!strcmp(dirname, "/") && !strcmp(basename, "")) {
  1092. printf("Error: cannot remove root\n");
  1093. ret = -EINVAL;
  1094. goto exit;
  1095. }
  1096. itr = malloc_cache_aligned(sizeof(fat_itr));
  1097. if (!itr) {
  1098. printf("Error: allocating memory\n");
  1099. ret = -ENOMEM;
  1100. goto exit;
  1101. }
  1102. ret = fat_itr_root(itr, &fsdata);
  1103. if (ret)
  1104. goto exit;
  1105. total_sector = fsdata.total_sect;
  1106. ret = fat_itr_resolve(itr, dirname, TYPE_DIR);
  1107. if (ret) {
  1108. printf("%s: doesn't exist (%d)\n", dirname, ret);
  1109. ret = -ENOENT;
  1110. goto exit;
  1111. }
  1112. if (!find_directory_entry(itr, basename)) {
  1113. printf("%s: doesn't exist\n", basename);
  1114. ret = -ENOENT;
  1115. goto exit;
  1116. }
  1117. if (fat_itr_isdir(itr)) {
  1118. n_entries = fat_dir_entries(itr);
  1119. if (n_entries < 0) {
  1120. ret = n_entries;
  1121. goto exit;
  1122. }
  1123. if (n_entries > 2) {
  1124. printf("Error: directory is not empty: %d\n",
  1125. n_entries);
  1126. ret = -EINVAL;
  1127. goto exit;
  1128. }
  1129. }
  1130. ret = delete_dentry(itr);
  1131. exit:
  1132. free(fsdata.fatbuf);
  1133. free(itr);
  1134. free(filename_copy);
  1135. return ret;
  1136. }
  1137. int fat_mkdir(const char *new_dirname)
  1138. {
  1139. dir_entry *retdent;
  1140. fsdata datablock = { .fatbuf = NULL, };
  1141. fsdata *mydata = &datablock;
  1142. fat_itr *itr = NULL;
  1143. char *dirname_copy, *parent, *dirname;
  1144. char l_dirname[VFAT_MAXLEN_BYTES];
  1145. int ret = -1;
  1146. loff_t actwrite;
  1147. unsigned int bytesperclust;
  1148. dir_entry *dotdent = NULL;
  1149. dirname_copy = strdup(new_dirname);
  1150. if (!dirname_copy)
  1151. goto exit;
  1152. split_filename(dirname_copy, &parent, &dirname);
  1153. if (!strlen(dirname)) {
  1154. ret = -EINVAL;
  1155. goto exit;
  1156. }
  1157. if (normalize_longname(l_dirname, dirname)) {
  1158. printf("FAT: illegal filename (%s)\n", dirname);
  1159. ret = -EINVAL;
  1160. goto exit;
  1161. }
  1162. itr = malloc_cache_aligned(sizeof(fat_itr));
  1163. if (!itr) {
  1164. ret = -ENOMEM;
  1165. goto exit;
  1166. }
  1167. ret = fat_itr_root(itr, &datablock);
  1168. if (ret)
  1169. goto exit;
  1170. total_sector = datablock.total_sect;
  1171. ret = fat_itr_resolve(itr, parent, TYPE_DIR);
  1172. if (ret) {
  1173. printf("%s: doesn't exist (%d)\n", parent, ret);
  1174. goto exit;
  1175. }
  1176. retdent = find_directory_entry(itr, l_dirname);
  1177. if (retdent) {
  1178. printf("%s: already exists\n", l_dirname);
  1179. ret = -EEXIST;
  1180. goto exit;
  1181. } else {
  1182. if (itr->is_root) {
  1183. /* root dir cannot have "." or ".." */
  1184. if (!strcmp(l_dirname, ".") ||
  1185. !strcmp(l_dirname, "..")) {
  1186. ret = -EINVAL;
  1187. goto exit;
  1188. }
  1189. }
  1190. if (!itr->dent) {
  1191. printf("Error: allocating new dir entry\n");
  1192. ret = -EIO;
  1193. goto exit;
  1194. }
  1195. memset(itr->dent, 0, sizeof(*itr->dent));
  1196. /* Set short name to set alias checksum field in dir_slot */
  1197. set_name(itr->dent, dirname);
  1198. fill_dir_slot(itr, dirname);
  1199. /* Set attribute as archive for regular file */
  1200. fill_dentry(itr->fsdata, itr->dent, dirname, 0, 0,
  1201. ATTR_DIR | ATTR_ARCH);
  1202. retdent = itr->dent;
  1203. }
  1204. /* Default entries */
  1205. bytesperclust = mydata->clust_size * mydata->sect_size;
  1206. dotdent = malloc_cache_aligned(bytesperclust);
  1207. if (!dotdent) {
  1208. ret = -ENOMEM;
  1209. goto exit;
  1210. }
  1211. memset(dotdent, 0, bytesperclust);
  1212. memcpy(dotdent[0].name, ". ", 8);
  1213. memcpy(dotdent[0].ext, " ", 3);
  1214. dotdent[0].attr = ATTR_DIR | ATTR_ARCH;
  1215. memcpy(dotdent[1].name, ".. ", 8);
  1216. memcpy(dotdent[1].ext, " ", 3);
  1217. dotdent[1].attr = ATTR_DIR | ATTR_ARCH;
  1218. set_start_cluster(mydata, &dotdent[1], itr->start_clust);
  1219. ret = set_contents(mydata, retdent, 0, (__u8 *)dotdent,
  1220. bytesperclust, &actwrite);
  1221. if (ret < 0) {
  1222. printf("Error: writing contents\n");
  1223. goto exit;
  1224. }
  1225. /* Write twice for "." */
  1226. set_start_cluster(mydata, &dotdent[0], START(retdent));
  1227. ret = set_contents(mydata, retdent, 0, (__u8 *)dotdent,
  1228. bytesperclust, &actwrite);
  1229. if (ret < 0) {
  1230. printf("Error: writing contents\n");
  1231. goto exit;
  1232. }
  1233. /* Flush fat buffer */
  1234. ret = flush_dirty_fat_buffer(mydata);
  1235. if (ret) {
  1236. printf("Error: flush fat buffer\n");
  1237. goto exit;
  1238. }
  1239. /* Write directory table to device */
  1240. ret = flush_dir(itr);
  1241. if (ret)
  1242. printf("Error: writing directory entry\n");
  1243. exit:
  1244. free(dirname_copy);
  1245. free(mydata->fatbuf);
  1246. free(itr);
  1247. free(dotdent);
  1248. return ret;
  1249. }