fat.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328
  1. /* fat.c - FAT filesystem */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2000,2001,2002,2003,2004,2005,2007,2008,2009 Free Software Foundation, Inc.
  5. *
  6. * GRUB is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * GRUB is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <grub/fs.h>
  20. #include <grub/disk.h>
  21. #include <grub/file.h>
  22. #include <grub/types.h>
  23. #include <grub/misc.h>
  24. #include <grub/mm.h>
  25. #include <grub/err.h>
  26. #include <grub/dl.h>
  27. #include <grub/charset.h>
  28. #include <grub/datetime.h>
  29. #ifndef MODE_EXFAT
  30. #include <grub/fat.h>
  31. #else
  32. #include <grub/exfat.h>
  33. #endif
  34. #include <grub/fshelp.h>
  35. #include <grub/i18n.h>
  36. GRUB_MOD_LICENSE ("GPLv3+");
  37. enum
  38. {
  39. GRUB_FAT_ATTR_READ_ONLY = 0x01,
  40. GRUB_FAT_ATTR_HIDDEN = 0x02,
  41. GRUB_FAT_ATTR_SYSTEM = 0x04,
  42. #ifndef MODE_EXFAT
  43. GRUB_FAT_ATTR_VOLUME_ID = 0x08,
  44. #endif
  45. GRUB_FAT_ATTR_DIRECTORY = 0x10,
  46. GRUB_FAT_ATTR_ARCHIVE = 0x20,
  47. #ifndef MODE_EXFAT
  48. GRUB_FAT_ATTR_LONG_NAME = (GRUB_FAT_ATTR_READ_ONLY
  49. | GRUB_FAT_ATTR_HIDDEN
  50. | GRUB_FAT_ATTR_SYSTEM
  51. | GRUB_FAT_ATTR_VOLUME_ID),
  52. #endif
  53. GRUB_FAT_ATTR_VALID = (GRUB_FAT_ATTR_READ_ONLY
  54. | GRUB_FAT_ATTR_HIDDEN
  55. | GRUB_FAT_ATTR_SYSTEM
  56. | GRUB_FAT_ATTR_DIRECTORY
  57. | GRUB_FAT_ATTR_ARCHIVE
  58. #ifndef MODE_EXFAT
  59. | GRUB_FAT_ATTR_VOLUME_ID
  60. #endif
  61. )
  62. };
  63. #ifdef MODE_EXFAT
  64. typedef struct grub_exfat_bpb grub_current_fat_bpb_t;
  65. #else
  66. typedef struct grub_fat_bpb grub_current_fat_bpb_t;
  67. #endif
  68. #ifdef MODE_EXFAT
  69. enum
  70. {
  71. FLAG_CONTIGUOUS = 2
  72. };
  73. struct grub_fat_dir_entry
  74. {
  75. grub_uint8_t entry_type;
  76. union
  77. {
  78. grub_uint8_t placeholder[31];
  79. struct {
  80. grub_uint8_t secondary_count;
  81. grub_uint16_t checksum;
  82. grub_uint16_t attr;
  83. grub_uint16_t reserved1;
  84. grub_uint32_t c_time;
  85. grub_uint32_t m_time;
  86. grub_uint32_t a_time;
  87. grub_uint8_t c_time_tenth;
  88. grub_uint8_t m_time_tenth;
  89. grub_uint8_t a_time_tenth;
  90. grub_uint8_t reserved2[9];
  91. } GRUB_PACKED file;
  92. struct {
  93. grub_uint8_t flags;
  94. grub_uint8_t reserved1;
  95. grub_uint8_t name_length;
  96. grub_uint16_t name_hash;
  97. grub_uint16_t reserved2;
  98. grub_uint64_t valid_size;
  99. grub_uint32_t reserved3;
  100. grub_uint32_t first_cluster;
  101. grub_uint64_t file_size;
  102. } GRUB_PACKED stream_extension;
  103. struct {
  104. grub_uint8_t flags;
  105. grub_uint16_t str[15];
  106. } GRUB_PACKED file_name;
  107. struct {
  108. grub_uint8_t character_count;
  109. grub_uint16_t str[15];
  110. } GRUB_PACKED volume_label;
  111. } GRUB_PACKED type_specific;
  112. } GRUB_PACKED;
  113. struct grub_fat_dir_node
  114. {
  115. grub_uint32_t attr;
  116. grub_uint32_t first_cluster;
  117. grub_uint64_t file_size;
  118. grub_uint64_t valid_size;
  119. int have_stream;
  120. int is_contiguous;
  121. };
  122. typedef struct grub_fat_dir_node grub_fat_dir_node_t;
  123. #else
  124. struct grub_fat_dir_entry
  125. {
  126. grub_uint8_t name[11];
  127. grub_uint8_t attr;
  128. grub_uint8_t nt_reserved;
  129. grub_uint8_t c_time_tenth;
  130. grub_uint16_t c_time;
  131. grub_uint16_t c_date;
  132. grub_uint16_t a_date;
  133. grub_uint16_t first_cluster_high;
  134. grub_uint16_t w_time;
  135. grub_uint16_t w_date;
  136. grub_uint16_t first_cluster_low;
  137. grub_uint32_t file_size;
  138. } GRUB_PACKED;
  139. struct grub_fat_long_name_entry
  140. {
  141. grub_uint8_t id;
  142. grub_uint16_t name1[5];
  143. grub_uint8_t attr;
  144. grub_uint8_t reserved;
  145. grub_uint8_t checksum;
  146. grub_uint16_t name2[6];
  147. grub_uint16_t first_cluster;
  148. grub_uint16_t name3[2];
  149. } GRUB_PACKED;
  150. typedef struct grub_fat_dir_entry grub_fat_dir_node_t;
  151. #endif
  152. struct grub_fat_data
  153. {
  154. int logical_sector_bits;
  155. grub_uint32_t num_sectors;
  156. grub_uint32_t fat_sector;
  157. grub_uint32_t sectors_per_fat;
  158. int fat_size;
  159. grub_uint32_t root_cluster;
  160. #ifndef MODE_EXFAT
  161. grub_uint32_t root_sector;
  162. grub_uint32_t num_root_sectors;
  163. #endif
  164. int cluster_bits;
  165. grub_uint32_t cluster_eof_mark;
  166. grub_uint32_t cluster_sector;
  167. grub_uint32_t num_clusters;
  168. grub_uint32_t uuid;
  169. };
  170. struct grub_fshelp_node {
  171. grub_disk_t disk;
  172. struct grub_fat_data *data;
  173. grub_uint8_t attr;
  174. #ifndef MODE_EXFAT
  175. grub_uint32_t file_size;
  176. #else
  177. grub_uint64_t file_size;
  178. #endif
  179. grub_uint32_t file_cluster;
  180. grub_uint32_t cur_cluster_num;
  181. grub_uint32_t cur_cluster;
  182. #ifdef MODE_EXFAT
  183. int is_contiguous;
  184. #endif
  185. };
  186. static grub_dl_t my_mod;
  187. #ifndef MODE_EXFAT
  188. static int
  189. fat_log2 (unsigned x)
  190. {
  191. int i;
  192. if (x == 0)
  193. return -1;
  194. for (i = 0; (x & 1) == 0; i++)
  195. x >>= 1;
  196. if (x != 1)
  197. return -1;
  198. return i;
  199. }
  200. #endif
  201. static struct grub_fat_data *
  202. grub_fat_mount (grub_disk_t disk)
  203. {
  204. grub_current_fat_bpb_t bpb;
  205. struct grub_fat_data *data = 0;
  206. grub_uint32_t first_fat, magic;
  207. if (! disk)
  208. goto fail;
  209. data = (struct grub_fat_data *) grub_malloc (sizeof (*data));
  210. if (! data)
  211. goto fail;
  212. /* Read the BPB. */
  213. if (grub_disk_read (disk, 0, 0, sizeof (bpb), &bpb))
  214. goto fail;
  215. #ifdef MODE_EXFAT
  216. if (grub_memcmp ((const char *) bpb.oem_name, "EXFAT ",
  217. sizeof (bpb.oem_name)) != 0)
  218. goto fail;
  219. #endif
  220. /* Get the sizes of logical sectors and clusters. */
  221. #ifdef MODE_EXFAT
  222. data->logical_sector_bits = bpb.bytes_per_sector_shift;
  223. #else
  224. data->logical_sector_bits =
  225. fat_log2 (grub_le_to_cpu16 (bpb.bytes_per_sector));
  226. #endif
  227. if (data->logical_sector_bits < GRUB_DISK_SECTOR_BITS
  228. || data->logical_sector_bits >= 16)
  229. goto fail;
  230. data->logical_sector_bits -= GRUB_DISK_SECTOR_BITS;
  231. #ifdef MODE_EXFAT
  232. data->cluster_bits = bpb.sectors_per_cluster_shift;
  233. #else
  234. data->cluster_bits = fat_log2 (bpb.sectors_per_cluster);
  235. #endif
  236. if (data->cluster_bits < 0 || data->cluster_bits > 25)
  237. goto fail;
  238. data->cluster_bits += data->logical_sector_bits;
  239. /* Get information about FATs. */
  240. #ifdef MODE_EXFAT
  241. data->fat_sector = (grub_le_to_cpu32 (bpb.num_reserved_sectors)
  242. << data->logical_sector_bits);
  243. #else
  244. data->fat_sector = (grub_le_to_cpu16 (bpb.num_reserved_sectors)
  245. << data->logical_sector_bits);
  246. #endif
  247. if (data->fat_sector == 0)
  248. goto fail;
  249. #ifdef MODE_EXFAT
  250. data->sectors_per_fat = (grub_le_to_cpu32 (bpb.sectors_per_fat)
  251. << data->logical_sector_bits);
  252. #else
  253. data->sectors_per_fat = ((bpb.sectors_per_fat_16
  254. ? grub_le_to_cpu16 (bpb.sectors_per_fat_16)
  255. : grub_le_to_cpu32 (bpb.version_specific.fat32.sectors_per_fat_32))
  256. << data->logical_sector_bits);
  257. #endif
  258. if (data->sectors_per_fat == 0)
  259. goto fail;
  260. /* Get the number of sectors in this volume. */
  261. #ifdef MODE_EXFAT
  262. data->num_sectors = ((grub_le_to_cpu64 (bpb.num_total_sectors))
  263. << data->logical_sector_bits);
  264. #else
  265. data->num_sectors = ((bpb.num_total_sectors_16
  266. ? grub_le_to_cpu16 (bpb.num_total_sectors_16)
  267. : grub_le_to_cpu32 (bpb.num_total_sectors_32))
  268. << data->logical_sector_bits);
  269. #endif
  270. if (data->num_sectors == 0)
  271. goto fail;
  272. /* Get information about the root directory. */
  273. if (bpb.num_fats == 0)
  274. goto fail;
  275. #ifndef MODE_EXFAT
  276. data->root_sector = data->fat_sector + bpb.num_fats * data->sectors_per_fat;
  277. data->num_root_sectors
  278. = ((((grub_uint32_t) grub_le_to_cpu16 (bpb.num_root_entries)
  279. * sizeof (struct grub_fat_dir_entry)
  280. + grub_le_to_cpu16 (bpb.bytes_per_sector) - 1)
  281. >> (data->logical_sector_bits + GRUB_DISK_SECTOR_BITS))
  282. << (data->logical_sector_bits));
  283. #endif
  284. #ifdef MODE_EXFAT
  285. data->cluster_sector = (grub_le_to_cpu32 (bpb.cluster_offset)
  286. << data->logical_sector_bits);
  287. data->num_clusters = (grub_le_to_cpu32 (bpb.cluster_count)
  288. << data->logical_sector_bits);
  289. #else
  290. data->cluster_sector = data->root_sector + data->num_root_sectors;
  291. data->num_clusters = (((data->num_sectors - data->cluster_sector)
  292. >> data->cluster_bits)
  293. + 2);
  294. #endif
  295. if (data->num_clusters <= 2)
  296. goto fail;
  297. #ifdef MODE_EXFAT
  298. {
  299. /* exFAT. */
  300. data->root_cluster = grub_le_to_cpu32 (bpb.root_cluster);
  301. data->fat_size = 32;
  302. data->cluster_eof_mark = 0xffffffff;
  303. if ((bpb.volume_flags & grub_cpu_to_le16_compile_time (0x1))
  304. && bpb.num_fats > 1)
  305. data->fat_sector += data->sectors_per_fat;
  306. }
  307. #else
  308. if (! bpb.sectors_per_fat_16)
  309. {
  310. /* FAT32. */
  311. grub_uint16_t flags = grub_le_to_cpu16 (bpb.version_specific.fat32.extended_flags);
  312. data->root_cluster = grub_le_to_cpu32 (bpb.version_specific.fat32.root_cluster);
  313. data->fat_size = 32;
  314. data->cluster_eof_mark = 0x0ffffff8;
  315. if (flags & 0x80)
  316. {
  317. /* Get an active FAT. */
  318. unsigned active_fat = flags & 0xf;
  319. if (active_fat > bpb.num_fats)
  320. goto fail;
  321. data->fat_sector += active_fat * data->sectors_per_fat;
  322. }
  323. if (bpb.num_root_entries != 0 || bpb.version_specific.fat32.fs_version != 0)
  324. goto fail;
  325. }
  326. else
  327. {
  328. /* FAT12 or FAT16. */
  329. data->root_cluster = ~0U;
  330. if (data->num_clusters <= 4085 + 2)
  331. {
  332. /* FAT12. */
  333. data->fat_size = 12;
  334. data->cluster_eof_mark = 0x0ff8;
  335. }
  336. else
  337. {
  338. /* FAT16. */
  339. data->fat_size = 16;
  340. data->cluster_eof_mark = 0xfff8;
  341. }
  342. }
  343. #endif
  344. /* More sanity checks. */
  345. if (data->num_sectors <= data->fat_sector)
  346. goto fail;
  347. if (grub_disk_read (disk,
  348. data->fat_sector,
  349. 0,
  350. sizeof (first_fat),
  351. &first_fat))
  352. goto fail;
  353. first_fat = grub_le_to_cpu32 (first_fat);
  354. if (data->fat_size == 32)
  355. {
  356. first_fat &= 0x0fffffff;
  357. magic = 0x0fffff00;
  358. }
  359. else if (data->fat_size == 16)
  360. {
  361. first_fat &= 0x0000ffff;
  362. magic = 0xff00;
  363. }
  364. else
  365. {
  366. first_fat &= 0x00000fff;
  367. magic = 0x0f00;
  368. }
  369. /* Serial number. */
  370. #ifdef MODE_EXFAT
  371. data->uuid = grub_le_to_cpu32 (bpb.num_serial);
  372. #else
  373. if (bpb.sectors_per_fat_16)
  374. data->uuid = grub_le_to_cpu32 (bpb.version_specific.fat12_or_fat16.num_serial);
  375. else
  376. data->uuid = grub_le_to_cpu32 (bpb.version_specific.fat32.num_serial);
  377. #endif
  378. #ifndef MODE_EXFAT
  379. /* Ignore the 3rd bit, because some BIOSes assigns 0xF0 to the media
  380. descriptor, even if it is a so-called superfloppy (e.g. an USB key).
  381. The check may be too strict for this kind of stupid BIOSes, as
  382. they overwrite the media descriptor. */
  383. if ((first_fat | 0x8) != (magic | bpb.media | 0x8))
  384. goto fail;
  385. #else
  386. (void) magic;
  387. #endif
  388. return data;
  389. fail:
  390. grub_free (data);
  391. grub_error (GRUB_ERR_BAD_FS, "not a FAT filesystem");
  392. return 0;
  393. }
  394. static grub_ssize_t
  395. grub_fat_read_data (grub_disk_t disk, grub_fshelp_node_t node,
  396. grub_disk_read_hook_t read_hook, void *read_hook_data,
  397. grub_off_t offset, grub_size_t len, char *buf)
  398. {
  399. grub_size_t size;
  400. grub_uint32_t logical_cluster;
  401. unsigned logical_cluster_bits;
  402. grub_ssize_t ret = 0;
  403. unsigned long sector;
  404. #ifndef MODE_EXFAT
  405. /* This is a special case. FAT12 and FAT16 doesn't have the root directory
  406. in clusters. */
  407. if (node->file_cluster == ~0U)
  408. {
  409. size = (node->data->num_root_sectors << GRUB_DISK_SECTOR_BITS) - offset;
  410. if (size > len)
  411. size = len;
  412. if (grub_disk_read (disk, node->data->root_sector, offset, size, buf))
  413. return -1;
  414. return size;
  415. }
  416. #endif
  417. #ifdef MODE_EXFAT
  418. if (node->is_contiguous)
  419. {
  420. /* Read the data here. */
  421. sector = (node->data->cluster_sector
  422. + ((node->file_cluster - 2)
  423. << node->data->cluster_bits));
  424. disk->read_hook = read_hook;
  425. disk->read_hook_data = read_hook_data;
  426. grub_disk_read (disk, sector + (offset >> GRUB_DISK_SECTOR_BITS),
  427. offset & (GRUB_DISK_SECTOR_SIZE - 1), len, buf);
  428. disk->read_hook = 0;
  429. if (grub_errno)
  430. return -1;
  431. return len;
  432. }
  433. #endif
  434. /* Calculate the logical cluster number and offset. */
  435. logical_cluster_bits = (node->data->cluster_bits
  436. + GRUB_DISK_SECTOR_BITS);
  437. logical_cluster = offset >> logical_cluster_bits;
  438. offset &= (1ULL << logical_cluster_bits) - 1;
  439. if (logical_cluster < node->cur_cluster_num)
  440. {
  441. node->cur_cluster_num = 0;
  442. node->cur_cluster = node->file_cluster;
  443. }
  444. while (len)
  445. {
  446. while (logical_cluster > node->cur_cluster_num)
  447. {
  448. /* Find next cluster. */
  449. grub_uint32_t next_cluster;
  450. grub_uint32_t fat_offset;
  451. switch (node->data->fat_size)
  452. {
  453. case 32:
  454. fat_offset = node->cur_cluster << 2;
  455. break;
  456. case 16:
  457. fat_offset = node->cur_cluster << 1;
  458. break;
  459. default:
  460. /* case 12: */
  461. fat_offset = node->cur_cluster + (node->cur_cluster >> 1);
  462. break;
  463. }
  464. /* Read the FAT. */
  465. if (grub_disk_read (disk, node->data->fat_sector, fat_offset,
  466. (node->data->fat_size + 7) >> 3,
  467. (char *) &next_cluster))
  468. return -1;
  469. next_cluster = grub_le_to_cpu32 (next_cluster);
  470. switch (node->data->fat_size)
  471. {
  472. case 16:
  473. next_cluster &= 0xFFFF;
  474. break;
  475. case 12:
  476. if (node->cur_cluster & 1)
  477. next_cluster >>= 4;
  478. next_cluster &= 0x0FFF;
  479. break;
  480. }
  481. grub_dprintf ("fat", "fat_size=%d, next_cluster=%u\n",
  482. node->data->fat_size, next_cluster);
  483. /* Check the end. */
  484. if (next_cluster >= node->data->cluster_eof_mark)
  485. return ret;
  486. if (next_cluster < 2 || next_cluster >= node->data->num_clusters)
  487. {
  488. grub_error (GRUB_ERR_BAD_FS, "invalid cluster %u",
  489. next_cluster);
  490. return -1;
  491. }
  492. node->cur_cluster = next_cluster;
  493. node->cur_cluster_num++;
  494. }
  495. /* Read the data here. */
  496. sector = (node->data->cluster_sector
  497. + ((node->cur_cluster - 2)
  498. << node->data->cluster_bits));
  499. size = (1 << logical_cluster_bits) - offset;
  500. if (size > len)
  501. size = len;
  502. disk->read_hook = read_hook;
  503. disk->read_hook_data = read_hook_data;
  504. grub_disk_read (disk, sector, offset, size, buf);
  505. disk->read_hook = 0;
  506. if (grub_errno)
  507. return -1;
  508. len -= size;
  509. buf += size;
  510. ret += size;
  511. logical_cluster++;
  512. offset = 0;
  513. }
  514. return ret;
  515. }
  516. struct grub_fat_iterate_context
  517. {
  518. #ifdef MODE_EXFAT
  519. struct grub_fat_dir_node dir;
  520. struct grub_fat_dir_entry entry;
  521. #else
  522. struct grub_fat_dir_entry dir;
  523. #endif
  524. char *filename;
  525. grub_uint16_t *unibuf;
  526. grub_ssize_t offset;
  527. };
  528. static grub_err_t
  529. grub_fat_iterate_init (struct grub_fat_iterate_context *ctxt)
  530. {
  531. ctxt->offset = -sizeof (struct grub_fat_dir_entry);
  532. #ifndef MODE_EXFAT
  533. /* Allocate space enough to hold a long name. */
  534. ctxt->filename = grub_malloc (0x40 * 13 * GRUB_MAX_UTF8_PER_UTF16 + 1);
  535. ctxt->unibuf = (grub_uint16_t *) grub_malloc (0x40 * 13 * 2);
  536. #else
  537. ctxt->unibuf = grub_malloc (15 * 256 * 2);
  538. ctxt->filename = grub_malloc (15 * 256 * GRUB_MAX_UTF8_PER_UTF16 + 1);
  539. #endif
  540. if (! ctxt->filename || ! ctxt->unibuf)
  541. {
  542. grub_free (ctxt->filename);
  543. grub_free (ctxt->unibuf);
  544. return grub_errno;
  545. }
  546. return GRUB_ERR_NONE;
  547. }
  548. static void
  549. grub_fat_iterate_fini (struct grub_fat_iterate_context *ctxt)
  550. {
  551. grub_free (ctxt->filename);
  552. grub_free (ctxt->unibuf);
  553. }
  554. #ifdef MODE_EXFAT
  555. static grub_err_t
  556. grub_fat_iterate_dir_next (grub_fshelp_node_t node,
  557. struct grub_fat_iterate_context *ctxt)
  558. {
  559. grub_memset (&ctxt->dir, 0, sizeof (ctxt->dir));
  560. while (1)
  561. {
  562. struct grub_fat_dir_entry *dir = &ctxt->entry;
  563. ctxt->offset += sizeof (*dir);
  564. if (grub_fat_read_data (node->disk, node, 0, 0, ctxt->offset, sizeof (*dir),
  565. (char *) dir)
  566. != sizeof (*dir))
  567. break;
  568. if (dir->entry_type == 0)
  569. break;
  570. if (!(dir->entry_type & 0x80))
  571. continue;
  572. if (dir->entry_type == 0x85)
  573. {
  574. unsigned i, nsec, slots = 0;
  575. nsec = dir->type_specific.file.secondary_count;
  576. ctxt->dir.attr = grub_cpu_to_le16 (dir->type_specific.file.attr);
  577. ctxt->dir.have_stream = 0;
  578. for (i = 0; i < nsec; i++)
  579. {
  580. struct grub_fat_dir_entry sec;
  581. ctxt->offset += sizeof (sec);
  582. if (grub_fat_read_data (node->disk, node, 0, 0,
  583. ctxt->offset, sizeof (sec), (char *) &sec)
  584. != sizeof (sec))
  585. break;
  586. if (!(sec.entry_type & 0x80))
  587. continue;
  588. if (!(sec.entry_type & 0x40))
  589. break;
  590. switch (sec.entry_type)
  591. {
  592. case 0xc0:
  593. ctxt->dir.first_cluster = grub_cpu_to_le32 (sec.type_specific.stream_extension.first_cluster);
  594. ctxt->dir.valid_size
  595. = grub_cpu_to_le64 (sec.type_specific.stream_extension.valid_size);
  596. ctxt->dir.file_size
  597. = grub_cpu_to_le64 (sec.type_specific.stream_extension.file_size);
  598. ctxt->dir.have_stream = 1;
  599. ctxt->dir.is_contiguous = !!(sec.type_specific.stream_extension.flags
  600. & grub_cpu_to_le16_compile_time (FLAG_CONTIGUOUS));
  601. break;
  602. case 0xc1:
  603. {
  604. int j;
  605. for (j = 0; j < 15; j++)
  606. ctxt->unibuf[slots * 15 + j]
  607. = grub_le_to_cpu16 (sec.type_specific.file_name.str[j]);
  608. slots++;
  609. }
  610. break;
  611. default:
  612. grub_dprintf ("exfat", "unknown secondary type 0x%02x\n",
  613. sec.entry_type);
  614. }
  615. }
  616. if (i != nsec)
  617. {
  618. ctxt->offset -= sizeof (*dir);
  619. continue;
  620. }
  621. *grub_utf16_to_utf8 ((grub_uint8_t *) ctxt->filename, ctxt->unibuf,
  622. slots * 15) = '\0';
  623. return 0;
  624. }
  625. /* Allocation bitmap. */
  626. if (dir->entry_type == 0x81)
  627. continue;
  628. /* Upcase table. */
  629. if (dir->entry_type == 0x82)
  630. continue;
  631. /* Volume label. */
  632. if (dir->entry_type == 0x83)
  633. continue;
  634. grub_dprintf ("exfat", "unknown primary type 0x%02x\n",
  635. dir->entry_type);
  636. }
  637. return grub_errno ? : GRUB_ERR_EOF;
  638. }
  639. /*
  640. * Convert a timestamp in exFAT format to seconds since the UNIX epoch
  641. * according to sections 7.4.8 and 7.4.9 in the exFAT specification.
  642. * https://docs.microsoft.com/en-us/windows/win32/fileio/exfat-specification
  643. */
  644. static int
  645. grub_exfat_timestamp (grub_uint32_t field, grub_uint8_t msec, grub_int64_t *nix) {
  646. struct grub_datetime datetime = {
  647. .year = (field >> 25) + 1980,
  648. .month = (field & 0x01E00000) >> 21,
  649. .day = (field & 0x001F0000) >> 16,
  650. .hour = (field & 0x0000F800) >> 11,
  651. .minute = (field & 0x000007E0) >> 5,
  652. .second = (field & 0x0000001F) * 2 + (msec >= 100 ? 1 : 0),
  653. };
  654. /* The conversion below allows seconds=60, so don't trust its validation. */
  655. if ((field & 0x1F) > 29)
  656. return 0;
  657. /* Validate the 10-msec field even though it is rounded down to seconds. */
  658. if (msec > 199)
  659. return 0;
  660. return grub_datetime2unixtime (&datetime, nix);
  661. }
  662. #else
  663. static grub_err_t
  664. grub_fat_iterate_dir_next (grub_fshelp_node_t node,
  665. struct grub_fat_iterate_context *ctxt)
  666. {
  667. char *filep = 0;
  668. int checksum = -1;
  669. int slot = -1, slots = -1;
  670. while (1)
  671. {
  672. unsigned i;
  673. /* Adjust the offset. */
  674. ctxt->offset += sizeof (ctxt->dir);
  675. /* Read a directory entry. */
  676. if (grub_fat_read_data (node->disk, node, 0, 0,
  677. ctxt->offset, sizeof (ctxt->dir),
  678. (char *) &ctxt->dir)
  679. != sizeof (ctxt->dir) || ctxt->dir.name[0] == 0)
  680. break;
  681. /* Handle long name entries. */
  682. if (ctxt->dir.attr == GRUB_FAT_ATTR_LONG_NAME)
  683. {
  684. struct grub_fat_long_name_entry *long_name
  685. = (struct grub_fat_long_name_entry *) &ctxt->dir;
  686. grub_uint8_t id = long_name->id;
  687. if (id & 0x40)
  688. {
  689. id &= 0x3f;
  690. slots = slot = id;
  691. checksum = long_name->checksum;
  692. }
  693. if (id != slot || slot == 0 || checksum != long_name->checksum)
  694. {
  695. checksum = -1;
  696. continue;
  697. }
  698. slot--;
  699. grub_memcpy (ctxt->unibuf + slot * 13, long_name->name1, 5 * 2);
  700. grub_memcpy (ctxt->unibuf + slot * 13 + 5, long_name->name2, 6 * 2);
  701. grub_memcpy (ctxt->unibuf + slot * 13 + 11, long_name->name3, 2 * 2);
  702. continue;
  703. }
  704. /* Check if this entry is valid. */
  705. if (ctxt->dir.name[0] == 0xe5 || (ctxt->dir.attr & ~GRUB_FAT_ATTR_VALID))
  706. continue;
  707. /* This is a workaround for Japanese. */
  708. if (ctxt->dir.name[0] == 0x05)
  709. ctxt->dir.name[0] = 0xe5;
  710. if (checksum != -1 && slot == 0)
  711. {
  712. grub_uint8_t sum;
  713. for (sum = 0, i = 0; i < sizeof (ctxt->dir.name); i++)
  714. sum = ((sum >> 1) | (sum << 7)) + ctxt->dir.name[i];
  715. if (sum == checksum)
  716. {
  717. int u;
  718. for (u = 0; u < slots * 13; u++)
  719. ctxt->unibuf[u] = grub_le_to_cpu16 (ctxt->unibuf[u]);
  720. *grub_utf16_to_utf8 ((grub_uint8_t *) ctxt->filename,
  721. ctxt->unibuf,
  722. slots * 13) = '\0';
  723. return GRUB_ERR_NONE;
  724. }
  725. checksum = -1;
  726. }
  727. /* Convert the 8.3 file name. */
  728. filep = ctxt->filename;
  729. if (ctxt->dir.attr & GRUB_FAT_ATTR_VOLUME_ID)
  730. {
  731. for (i = 0; i < sizeof (ctxt->dir.name) && ctxt->dir.name[i]; i++)
  732. *filep++ = ctxt->dir.name[i];
  733. while (i > 0 && ctxt->dir.name[i - 1] == ' ')
  734. {
  735. filep--;
  736. i--;
  737. }
  738. }
  739. else
  740. {
  741. for (i = 0; i < 8 && ctxt->dir.name[i]; i++)
  742. *filep++ = grub_tolower (ctxt->dir.name[i]);
  743. while (i > 0 && ctxt->dir.name[i - 1] == ' ')
  744. {
  745. filep--;
  746. i--;
  747. }
  748. /* XXX should we check that dir position is 0 or 1? */
  749. if (i > 2 || filep[0] != '.' || (i == 2 && filep[1] != '.'))
  750. *filep++ = '.';
  751. for (i = 8; i < 11 && ctxt->dir.name[i]; i++)
  752. *filep++ = grub_tolower (ctxt->dir.name[i]);
  753. while (i > 8 && ctxt->dir.name[i - 1] == ' ')
  754. {
  755. filep--;
  756. i--;
  757. }
  758. if (i == 8)
  759. filep--;
  760. }
  761. *filep = '\0';
  762. return GRUB_ERR_NONE;
  763. }
  764. return grub_errno ? : GRUB_ERR_EOF;
  765. }
  766. /*
  767. * Convert a date and time in FAT format to seconds since the UNIX epoch
  768. * according to sections 11.3.5 and 11.3.6 in ECMA-107.
  769. * https://www.ecma-international.org/publications/files/ECMA-ST/Ecma-107.pdf
  770. */
  771. static int
  772. grub_fat_timestamp (grub_uint16_t time, grub_uint16_t date, grub_int64_t *nix) {
  773. struct grub_datetime datetime = {
  774. .year = (date >> 9) + 1980,
  775. .month = (date & 0x01E0) >> 5,
  776. .day = (date & 0x001F),
  777. .hour = (time >> 11),
  778. .minute = (time & 0x07E0) >> 5,
  779. .second = (time & 0x001F) * 2,
  780. };
  781. /* The conversion below allows seconds=60, so don't trust its validation. */
  782. if ((time & 0x1F) > 29)
  783. return 0;
  784. return grub_datetime2unixtime (&datetime, nix);
  785. }
  786. #endif
  787. static grub_err_t lookup_file (grub_fshelp_node_t node,
  788. const char *name,
  789. grub_fshelp_node_t *foundnode,
  790. enum grub_fshelp_filetype *foundtype)
  791. {
  792. grub_err_t err;
  793. struct grub_fat_iterate_context ctxt;
  794. err = grub_fat_iterate_init (&ctxt);
  795. if (err)
  796. return err;
  797. while (!(err = grub_fat_iterate_dir_next (node, &ctxt)))
  798. {
  799. #ifdef MODE_EXFAT
  800. if (!ctxt.dir.have_stream)
  801. continue;
  802. #else
  803. if (ctxt.dir.attr & GRUB_FAT_ATTR_VOLUME_ID)
  804. continue;
  805. #endif
  806. if (grub_strcasecmp (name, ctxt.filename) == 0)
  807. {
  808. *foundnode = grub_malloc (sizeof (struct grub_fshelp_node));
  809. if (!*foundnode)
  810. return grub_errno;
  811. (*foundnode)->attr = ctxt.dir.attr;
  812. #ifdef MODE_EXFAT
  813. (*foundnode)->file_size = ctxt.dir.file_size;
  814. (*foundnode)->file_cluster = ctxt.dir.first_cluster;
  815. (*foundnode)->is_contiguous = ctxt.dir.is_contiguous;
  816. #else
  817. (*foundnode)->file_size = grub_le_to_cpu32 (ctxt.dir.file_size);
  818. (*foundnode)->file_cluster = ((grub_le_to_cpu16 (ctxt.dir.first_cluster_high) << 16)
  819. | grub_le_to_cpu16 (ctxt.dir.first_cluster_low));
  820. /* If directory points to root, starting cluster is 0 */
  821. if (!(*foundnode)->file_cluster)
  822. (*foundnode)->file_cluster = node->data->root_cluster;
  823. #endif
  824. (*foundnode)->cur_cluster_num = ~0U;
  825. (*foundnode)->data = node->data;
  826. (*foundnode)->disk = node->disk;
  827. *foundtype = ((*foundnode)->attr & GRUB_FAT_ATTR_DIRECTORY) ? GRUB_FSHELP_DIR : GRUB_FSHELP_REG;
  828. grub_fat_iterate_fini (&ctxt);
  829. return GRUB_ERR_NONE;
  830. }
  831. }
  832. grub_fat_iterate_fini (&ctxt);
  833. if (err == GRUB_ERR_EOF)
  834. err = 0;
  835. return err;
  836. }
  837. static grub_err_t
  838. grub_fat_dir (grub_device_t device, const char *path, grub_fs_dir_hook_t hook,
  839. void *hook_data)
  840. {
  841. struct grub_fat_data *data = 0;
  842. grub_disk_t disk = device->disk;
  843. grub_fshelp_node_t found = NULL;
  844. grub_err_t err;
  845. struct grub_fat_iterate_context ctxt;
  846. grub_dl_ref (my_mod);
  847. data = grub_fat_mount (disk);
  848. if (! data)
  849. goto fail;
  850. struct grub_fshelp_node root = {
  851. .data = data,
  852. .disk = disk,
  853. .attr = GRUB_FAT_ATTR_DIRECTORY,
  854. .file_size = 0,
  855. .file_cluster = data->root_cluster,
  856. .cur_cluster_num = ~0U,
  857. .cur_cluster = 0,
  858. #ifdef MODE_EXFAT
  859. .is_contiguous = 0,
  860. #endif
  861. };
  862. err = grub_fshelp_find_file_lookup (path, &root, &found, lookup_file, NULL, GRUB_FSHELP_DIR);
  863. if (err)
  864. goto fail;
  865. err = grub_fat_iterate_init (&ctxt);
  866. if (err)
  867. goto fail;
  868. while (!(err = grub_fat_iterate_dir_next (found, &ctxt)))
  869. {
  870. struct grub_dirhook_info info;
  871. grub_memset (&info, 0, sizeof (info));
  872. info.dir = !! (ctxt.dir.attr & GRUB_FAT_ATTR_DIRECTORY);
  873. info.case_insensitive = 1;
  874. #ifdef MODE_EXFAT
  875. if (!ctxt.dir.have_stream)
  876. continue;
  877. info.mtimeset = grub_exfat_timestamp (grub_le_to_cpu32 (ctxt.entry.type_specific.file.m_time),
  878. ctxt.entry.type_specific.file.m_time_tenth,
  879. &info.mtime);
  880. #else
  881. if (ctxt.dir.attr & GRUB_FAT_ATTR_VOLUME_ID)
  882. continue;
  883. info.mtimeset = grub_fat_timestamp (grub_le_to_cpu16 (ctxt.dir.w_time),
  884. grub_le_to_cpu16 (ctxt.dir.w_date),
  885. &info.mtime);
  886. #endif
  887. if (hook (ctxt.filename, &info, hook_data))
  888. break;
  889. }
  890. grub_fat_iterate_fini (&ctxt);
  891. if (err == GRUB_ERR_EOF)
  892. err = 0;
  893. fail:
  894. if (found != &root)
  895. grub_free (found);
  896. grub_free (data);
  897. grub_dl_unref (my_mod);
  898. return grub_errno;
  899. }
  900. static grub_err_t
  901. grub_fat_open (grub_file_t file, const char *name)
  902. {
  903. struct grub_fat_data *data = 0;
  904. grub_fshelp_node_t found = NULL;
  905. grub_err_t err;
  906. grub_disk_t disk = file->device->disk;
  907. grub_dl_ref (my_mod);
  908. data = grub_fat_mount (disk);
  909. if (! data)
  910. goto fail;
  911. struct grub_fshelp_node root = {
  912. .data = data,
  913. .disk = disk,
  914. .attr = GRUB_FAT_ATTR_DIRECTORY,
  915. .file_size = 0,
  916. .file_cluster = data->root_cluster,
  917. .cur_cluster_num = ~0U,
  918. .cur_cluster = 0,
  919. #ifdef MODE_EXFAT
  920. .is_contiguous = 0,
  921. #endif
  922. };
  923. err = grub_fshelp_find_file_lookup (name, &root, &found, lookup_file, NULL, GRUB_FSHELP_REG);
  924. if (err)
  925. goto fail;
  926. file->data = found;
  927. file->size = found->file_size;
  928. return GRUB_ERR_NONE;
  929. fail:
  930. if (found != &root)
  931. grub_free (found);
  932. grub_free (data);
  933. grub_dl_unref (my_mod);
  934. return grub_errno;
  935. }
  936. static grub_ssize_t
  937. grub_fat_read (grub_file_t file, char *buf, grub_size_t len)
  938. {
  939. return grub_fat_read_data (file->device->disk, file->data,
  940. file->read_hook, file->read_hook_data,
  941. file->offset, len, buf);
  942. }
  943. static grub_err_t
  944. grub_fat_close (grub_file_t file)
  945. {
  946. grub_fshelp_node_t node = file->data;
  947. grub_free (node->data);
  948. grub_free (node);
  949. grub_dl_unref (my_mod);
  950. return grub_errno;
  951. }
  952. #ifdef MODE_EXFAT
  953. static grub_err_t
  954. grub_fat_label (grub_device_t device, char **label)
  955. {
  956. struct grub_fat_dir_entry dir;
  957. grub_ssize_t offset = -sizeof(dir);
  958. grub_disk_t disk = device->disk;
  959. struct grub_fshelp_node root = {
  960. .disk = disk,
  961. .attr = GRUB_FAT_ATTR_DIRECTORY,
  962. .file_size = 0,
  963. .cur_cluster_num = ~0U,
  964. .cur_cluster = 0,
  965. .is_contiguous = 0,
  966. };
  967. root.data = grub_fat_mount (disk);
  968. if (! root.data)
  969. return grub_errno;
  970. root.file_cluster = root.data->root_cluster;
  971. *label = NULL;
  972. while (1)
  973. {
  974. offset += sizeof (dir);
  975. if (grub_fat_read_data (disk, &root, 0, 0,
  976. offset, sizeof (dir), (char *) &dir)
  977. != sizeof (dir))
  978. break;
  979. if (dir.entry_type == 0)
  980. break;
  981. if (!(dir.entry_type & 0x80))
  982. continue;
  983. /* Volume label. */
  984. if (dir.entry_type == 0x83)
  985. {
  986. grub_size_t chc;
  987. grub_uint16_t t[ARRAY_SIZE (dir.type_specific.volume_label.str)];
  988. grub_size_t i;
  989. *label = grub_malloc (ARRAY_SIZE (dir.type_specific.volume_label.str)
  990. * GRUB_MAX_UTF8_PER_UTF16 + 1);
  991. if (!*label)
  992. {
  993. grub_free (root.data);
  994. return grub_errno;
  995. }
  996. chc = dir.type_specific.volume_label.character_count;
  997. if (chc > ARRAY_SIZE (dir.type_specific.volume_label.str))
  998. chc = ARRAY_SIZE (dir.type_specific.volume_label.str);
  999. for (i = 0; i < chc; i++)
  1000. t[i] = grub_le_to_cpu16 (dir.type_specific.volume_label.str[i]);
  1001. *grub_utf16_to_utf8 ((grub_uint8_t *) *label, t, chc) = '\0';
  1002. }
  1003. }
  1004. grub_free (root.data);
  1005. return grub_errno;
  1006. }
  1007. #else
  1008. static grub_err_t
  1009. grub_fat_label (grub_device_t device, char **label)
  1010. {
  1011. grub_disk_t disk = device->disk;
  1012. grub_err_t err;
  1013. struct grub_fat_iterate_context ctxt;
  1014. struct grub_fshelp_node root = {
  1015. .disk = disk,
  1016. .attr = GRUB_FAT_ATTR_DIRECTORY,
  1017. .file_size = 0,
  1018. .cur_cluster_num = ~0U,
  1019. .cur_cluster = 0,
  1020. };
  1021. *label = 0;
  1022. grub_dl_ref (my_mod);
  1023. root.data = grub_fat_mount (disk);
  1024. if (! root.data)
  1025. goto fail;
  1026. root.file_cluster = root.data->root_cluster;
  1027. err = grub_fat_iterate_init (&ctxt);
  1028. if (err)
  1029. goto fail;
  1030. while (!(err = grub_fat_iterate_dir_next (&root, &ctxt)))
  1031. if ((ctxt.dir.attr & ~GRUB_FAT_ATTR_ARCHIVE) == GRUB_FAT_ATTR_VOLUME_ID)
  1032. {
  1033. *label = grub_strdup (ctxt.filename);
  1034. break;
  1035. }
  1036. grub_fat_iterate_fini (&ctxt);
  1037. fail:
  1038. grub_dl_unref (my_mod);
  1039. grub_free (root.data);
  1040. return grub_errno;
  1041. }
  1042. #endif
  1043. static grub_err_t
  1044. grub_fat_uuid (grub_device_t device, char **uuid)
  1045. {
  1046. struct grub_fat_data *data;
  1047. grub_disk_t disk = device->disk;
  1048. grub_dl_ref (my_mod);
  1049. data = grub_fat_mount (disk);
  1050. if (data)
  1051. {
  1052. char *ptr;
  1053. *uuid = grub_xasprintf ("%04x-%04x",
  1054. (grub_uint16_t) (data->uuid >> 16),
  1055. (grub_uint16_t) data->uuid);
  1056. for (ptr = *uuid; ptr && *ptr; ptr++)
  1057. *ptr = grub_toupper (*ptr);
  1058. }
  1059. else
  1060. *uuid = NULL;
  1061. grub_dl_unref (my_mod);
  1062. grub_free (data);
  1063. return grub_errno;
  1064. }
  1065. #ifdef GRUB_UTIL
  1066. #ifndef MODE_EXFAT
  1067. grub_disk_addr_t
  1068. grub_fat_get_cluster_sector (grub_disk_t disk, grub_uint64_t *sec_per_lcn)
  1069. #else
  1070. grub_disk_addr_t
  1071. grub_exfat_get_cluster_sector (grub_disk_t disk, grub_uint64_t *sec_per_lcn)
  1072. #endif
  1073. {
  1074. grub_disk_addr_t ret;
  1075. struct grub_fat_data *data;
  1076. data = grub_fat_mount (disk);
  1077. if (!data)
  1078. return 0;
  1079. ret = data->cluster_sector;
  1080. *sec_per_lcn = 1ULL << data->cluster_bits;
  1081. grub_free (data);
  1082. return ret;
  1083. }
  1084. #endif
  1085. static struct grub_fs grub_fat_fs =
  1086. {
  1087. #ifdef MODE_EXFAT
  1088. .name = "exfat",
  1089. #else
  1090. .name = "fat",
  1091. #endif
  1092. .fs_dir = grub_fat_dir,
  1093. .fs_open = grub_fat_open,
  1094. .fs_read = grub_fat_read,
  1095. .fs_close = grub_fat_close,
  1096. .fs_label = grub_fat_label,
  1097. .fs_uuid = grub_fat_uuid,
  1098. #ifdef GRUB_UTIL
  1099. #ifdef MODE_EXFAT
  1100. /* ExFAT BPB is 30 larger than FAT32 one. */
  1101. .reserved_first_sector = 0,
  1102. #else
  1103. .reserved_first_sector = 1,
  1104. #endif
  1105. .blocklist_install = 1,
  1106. #endif
  1107. .next = 0
  1108. };
  1109. #ifdef MODE_EXFAT
  1110. GRUB_MOD_INIT(exfat)
  1111. #else
  1112. GRUB_MOD_INIT(fat)
  1113. #endif
  1114. {
  1115. COMPILE_TIME_ASSERT (sizeof (struct grub_fat_dir_entry) == 32);
  1116. grub_fat_fs.mod = mod;
  1117. grub_fs_register (&grub_fat_fs);
  1118. my_mod = mod;
  1119. }
  1120. #ifdef MODE_EXFAT
  1121. GRUB_MOD_FINI(exfat)
  1122. #else
  1123. GRUB_MOD_FINI(fat)
  1124. #endif
  1125. {
  1126. grub_fs_unregister (&grub_fat_fs);
  1127. }