erofs.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001
  1. /* erofs.c - Enhanced Read-Only File System */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2024 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/disk.h>
  20. #include <grub/dl.h>
  21. #include <grub/err.h>
  22. #include <grub/file.h>
  23. #include <grub/fs.h>
  24. #include <grub/fshelp.h>
  25. #include <grub/misc.h>
  26. #include <grub/mm.h>
  27. #include <grub/safemath.h>
  28. #include <grub/types.h>
  29. GRUB_MOD_LICENSE ("GPLv3+");
  30. #define EROFS_SUPER_OFFSET 1024
  31. #define EROFS_MAGIC 0xE0F5E1E2
  32. #define EROFS_ISLOTBITS 5
  33. #define EROFS_FEATURE_INCOMPAT_CHUNKED_FILE 0x00000004
  34. #define EROFS_ALL_FEATURE_INCOMPAT EROFS_FEATURE_INCOMPAT_CHUNKED_FILE
  35. struct grub_erofs_super
  36. {
  37. grub_uint32_t magic;
  38. grub_uint32_t checksum;
  39. grub_uint32_t feature_compat;
  40. grub_uint8_t log2_blksz;
  41. grub_uint8_t sb_extslots;
  42. grub_uint16_t root_nid;
  43. grub_uint64_t inos;
  44. grub_uint64_t build_time;
  45. grub_uint32_t build_time_nsec;
  46. grub_uint32_t blocks;
  47. grub_uint32_t meta_blkaddr;
  48. grub_uint32_t xattr_blkaddr;
  49. grub_packed_guid_t uuid;
  50. grub_uint8_t volume_name[16];
  51. grub_uint32_t feature_incompat;
  52. union
  53. {
  54. grub_uint16_t available_compr_algs;
  55. grub_uint16_t lz4_max_distance;
  56. } GRUB_PACKED u1;
  57. grub_uint16_t extra_devices;
  58. grub_uint16_t devt_slotoff;
  59. grub_uint8_t log2_dirblksz;
  60. grub_uint8_t xattr_prefix_count;
  61. grub_uint32_t xattr_prefix_start;
  62. grub_uint64_t packed_nid;
  63. grub_uint8_t reserved2[24];
  64. } GRUB_PACKED;
  65. #define EROFS_INODE_LAYOUT_COMPACT 0
  66. #define EROFS_INODE_LAYOUT_EXTENDED 1
  67. #define EROFS_INODE_FLAT_PLAIN 0
  68. #define EROFS_INODE_COMPRESSED_FULL 1
  69. #define EROFS_INODE_FLAT_INLINE 2
  70. #define EROFS_INODE_COMPRESSED_COMPACT 3
  71. #define EROFS_INODE_CHUNK_BASED 4
  72. #define EROFS_I_VERSION_MASKS 0x01
  73. #define EROFS_I_DATALAYOUT_MASKS 0x07
  74. #define EROFS_I_VERSION_BIT 0
  75. #define EROFS_I_DATALAYOUT_BIT 1
  76. struct grub_erofs_inode_chunk_info
  77. {
  78. grub_uint16_t format;
  79. grub_uint16_t reserved;
  80. } GRUB_PACKED;
  81. #define EROFS_CHUNK_FORMAT_BLKBITS_MASK 0x001F
  82. #define EROFS_CHUNK_FORMAT_INDEXES 0x0020
  83. #define EROFS_BLOCK_MAP_ENTRY_SIZE 4
  84. #define EROFS_MAP_MAPPED 0x02
  85. #define EROFS_NULL_ADDR 1
  86. #define EROFS_NAME_LEN 255
  87. #define EROFS_PATH_LEN 4096
  88. #define EROFS_MIN_LOG2_BLOCK_SIZE 9
  89. #define EROFS_MAX_LOG2_BLOCK_SIZE 16
  90. struct grub_erofs_inode_chunk_index
  91. {
  92. grub_uint16_t advise;
  93. grub_uint16_t device_id;
  94. grub_uint32_t blkaddr;
  95. };
  96. union grub_erofs_inode_i_u
  97. {
  98. grub_uint32_t compressed_blocks;
  99. grub_uint32_t raw_blkaddr;
  100. grub_uint32_t rdev;
  101. struct grub_erofs_inode_chunk_info c;
  102. };
  103. struct grub_erofs_inode_compact
  104. {
  105. grub_uint16_t i_format;
  106. grub_uint16_t i_xattr_icount;
  107. grub_uint16_t i_mode;
  108. grub_uint16_t i_nlink;
  109. grub_uint32_t i_size;
  110. grub_uint32_t i_reserved;
  111. union grub_erofs_inode_i_u i_u;
  112. grub_uint32_t i_ino;
  113. grub_uint16_t i_uid;
  114. grub_uint16_t i_gid;
  115. grub_uint32_t i_reserved2;
  116. } GRUB_PACKED;
  117. struct grub_erofs_inode_extended
  118. {
  119. grub_uint16_t i_format;
  120. grub_uint16_t i_xattr_icount;
  121. grub_uint16_t i_mode;
  122. grub_uint16_t i_reserved;
  123. grub_uint64_t i_size;
  124. union grub_erofs_inode_i_u i_u;
  125. grub_uint32_t i_ino;
  126. grub_uint32_t i_uid;
  127. grub_uint32_t i_gid;
  128. grub_uint64_t i_mtime;
  129. grub_uint32_t i_mtime_nsec;
  130. grub_uint32_t i_nlink;
  131. grub_uint8_t i_reserved2[16];
  132. } GRUB_PACKED;
  133. union grub_erofs_inode
  134. {
  135. struct grub_erofs_inode_compact c;
  136. struct grub_erofs_inode_extended e;
  137. } GRUB_PACKED;
  138. #define EROFS_FT_UNKNOWN 0
  139. #define EROFS_FT_REG_FILE 1
  140. #define EROFS_FT_DIR 2
  141. #define EROFS_FT_CHRDEV 3
  142. #define EROFS_FT_BLKDEV 4
  143. #define EROFS_FT_FIFO 5
  144. #define EROFS_FT_SOCK 6
  145. #define EROFS_FT_SYMLINK 7
  146. struct grub_erofs_dirent
  147. {
  148. grub_uint64_t nid;
  149. grub_uint16_t nameoff;
  150. grub_uint8_t file_type;
  151. grub_uint8_t reserved;
  152. } GRUB_PACKED;
  153. struct grub_erofs_map_blocks
  154. {
  155. grub_uint64_t m_pa; /* physical address */
  156. grub_uint64_t m_la; /* logical address */
  157. grub_uint64_t m_plen; /* physical length */
  158. grub_uint64_t m_llen; /* logical length */
  159. grub_uint32_t m_flags;
  160. };
  161. struct grub_erofs_xattr_ibody_header
  162. {
  163. grub_uint32_t h_reserved;
  164. grub_uint8_t h_shared_count;
  165. grub_uint8_t h_reserved2[7];
  166. grub_uint32_t h_shared_xattrs[0];
  167. };
  168. struct grub_fshelp_node
  169. {
  170. struct grub_erofs_data *data;
  171. union grub_erofs_inode inode;
  172. grub_uint64_t ino;
  173. grub_uint8_t inode_type;
  174. grub_uint8_t inode_datalayout;
  175. /* If the inode has been read into memory? */
  176. bool inode_loaded;
  177. };
  178. struct grub_erofs_data
  179. {
  180. grub_disk_t disk;
  181. struct grub_erofs_super sb;
  182. struct grub_fshelp_node inode;
  183. };
  184. #define erofs_blocksz(data) (((grub_uint32_t) 1) << data->sb.log2_blksz)
  185. static grub_size_t
  186. grub_erofs_strnlen (const char *s, grub_size_t n)
  187. {
  188. const char *p = s;
  189. if (n == 0)
  190. return 0;
  191. while (n-- && *p)
  192. p++;
  193. return p - s;
  194. }
  195. static grub_uint64_t
  196. erofs_iloc (grub_fshelp_node_t node)
  197. {
  198. struct grub_erofs_super *sb = &node->data->sb;
  199. return ((grub_uint64_t) grub_le_to_cpu32 (sb->meta_blkaddr) << sb->log2_blksz) +
  200. (node->ino << EROFS_ISLOTBITS);
  201. }
  202. static grub_err_t
  203. erofs_read_inode (struct grub_erofs_data *data, grub_fshelp_node_t node)
  204. {
  205. union grub_erofs_inode *di;
  206. grub_err_t err;
  207. grub_uint16_t i_format;
  208. grub_uint64_t addr = erofs_iloc (node);
  209. di = (union grub_erofs_inode *) &node->inode;
  210. err = grub_disk_read (data->disk, addr >> GRUB_DISK_SECTOR_BITS,
  211. addr & (GRUB_DISK_SECTOR_SIZE - 1),
  212. sizeof (struct grub_erofs_inode_compact), &di->c);
  213. if (err != GRUB_ERR_NONE)
  214. return err;
  215. i_format = grub_le_to_cpu16 (di->c.i_format);
  216. node->inode_type = (i_format >> EROFS_I_VERSION_BIT) & EROFS_I_VERSION_MASKS;
  217. node->inode_datalayout = (i_format >> EROFS_I_DATALAYOUT_BIT) & EROFS_I_DATALAYOUT_MASKS;
  218. switch (node->inode_type)
  219. {
  220. case EROFS_INODE_LAYOUT_EXTENDED:
  221. addr += sizeof (struct grub_erofs_inode_compact);
  222. err = grub_disk_read (data->disk, addr >> GRUB_DISK_SECTOR_BITS,
  223. addr & (GRUB_DISK_SECTOR_SIZE - 1),
  224. sizeof (struct grub_erofs_inode_extended) - sizeof (struct grub_erofs_inode_compact),
  225. (grub_uint8_t *) di + sizeof (struct grub_erofs_inode_compact));
  226. if (err != GRUB_ERR_NONE)
  227. return err;
  228. break;
  229. case EROFS_INODE_LAYOUT_COMPACT:
  230. break;
  231. default:
  232. return grub_error (GRUB_ERR_BAD_FS, "invalid type %u @ inode %" PRIuGRUB_UINT64_T,
  233. node->inode_type, node->ino);
  234. }
  235. node->inode_loaded = true;
  236. return 0;
  237. }
  238. static grub_uint64_t
  239. erofs_inode_size (grub_fshelp_node_t node)
  240. {
  241. return node->inode_type == EROFS_INODE_LAYOUT_COMPACT
  242. ? sizeof (struct grub_erofs_inode_compact)
  243. : sizeof (struct grub_erofs_inode_extended);
  244. }
  245. static grub_uint64_t
  246. erofs_inode_file_size (grub_fshelp_node_t node)
  247. {
  248. union grub_erofs_inode *di = (union grub_erofs_inode *) &node->inode;
  249. return node->inode_type == EROFS_INODE_LAYOUT_COMPACT
  250. ? grub_le_to_cpu32 (di->c.i_size)
  251. : grub_le_to_cpu64 (di->e.i_size);
  252. }
  253. static grub_uint32_t
  254. erofs_inode_xattr_ibody_size (grub_fshelp_node_t node)
  255. {
  256. grub_uint16_t cnt = grub_le_to_cpu16 (node->inode.e.i_xattr_icount);
  257. if (cnt == 0)
  258. return 0;
  259. return sizeof (struct grub_erofs_xattr_ibody_header) + ((cnt - 1) * sizeof (grub_uint32_t));
  260. }
  261. static grub_uint64_t
  262. erofs_inode_mtime (grub_fshelp_node_t node)
  263. {
  264. return node->inode_type == EROFS_INODE_LAYOUT_COMPACT
  265. ? grub_le_to_cpu64 (node->data->sb.build_time)
  266. : grub_le_to_cpu64 (node->inode.e.i_mtime);
  267. }
  268. static grub_err_t
  269. erofs_map_blocks_flatmode (grub_fshelp_node_t node,
  270. struct grub_erofs_map_blocks *map)
  271. {
  272. grub_uint64_t nblocks, lastblk, file_size;
  273. bool tailendpacking = (node->inode_datalayout == EROFS_INODE_FLAT_INLINE);
  274. grub_uint64_t blocksz = erofs_blocksz (node->data);
  275. /* `file_size` is checked by caller and cannot be zero, hence nblocks > 0. */
  276. file_size = erofs_inode_file_size (node);
  277. if (grub_add (file_size, blocksz - 1, &nblocks))
  278. return grub_error (GRUB_ERR_OUT_OF_RANGE, "nblocks overflow");
  279. nblocks >>= node->data->sb.log2_blksz;
  280. lastblk = nblocks - tailendpacking;
  281. map->m_flags = EROFS_MAP_MAPPED;
  282. /* No overflow as (lastblk <= nblocks) && (nblocks * blocksz <= UINT64_MAX - blocksz + 1). */
  283. if (map->m_la < (lastblk * blocksz))
  284. {
  285. if (grub_mul ((grub_uint64_t) grub_le_to_cpu32 (node->inode.e.i_u.raw_blkaddr), blocksz, &map->m_pa) ||
  286. grub_add (map->m_pa, map->m_la, &map->m_pa))
  287. return grub_error (GRUB_ERR_OUT_OF_RANGE, "m_pa overflow");
  288. if (grub_sub (lastblk * blocksz, map->m_la, &map->m_plen))
  289. return grub_error (GRUB_ERR_OUT_OF_RANGE, "m_plen underflow");
  290. }
  291. else if (tailendpacking)
  292. {
  293. if (grub_add (erofs_iloc (node), erofs_inode_size (node), &map->m_pa) ||
  294. grub_add (map->m_pa, erofs_inode_xattr_ibody_size (node), &map->m_pa) ||
  295. grub_add (map->m_pa, map->m_la & (blocksz - 1), &map->m_pa))
  296. return grub_error (GRUB_ERR_OUT_OF_RANGE, "m_pa overflow when handling tailpacking");
  297. if (grub_sub (file_size, map->m_la, &map->m_plen))
  298. return grub_error (GRUB_ERR_OUT_OF_RANGE, "m_plen overflow when handling tailpacking");
  299. /* No overflow as map->m_plen <= UINT64_MAX - blocksz + 1. */
  300. if (((map->m_pa & (blocksz - 1)) + map->m_plen) > blocksz)
  301. return grub_error (GRUB_ERR_BAD_FS,
  302. "inline data cross block boundary @ inode %" PRIuGRUB_UINT64_T,
  303. node->ino);
  304. }
  305. else
  306. return grub_error (GRUB_ERR_BAD_FS,
  307. "invalid map->m_la=%" PRIuGRUB_UINT64_T
  308. " @ inode %" PRIuGRUB_UINT64_T,
  309. map->m_la, node->ino);
  310. map->m_llen = map->m_plen;
  311. return GRUB_ERR_NONE;
  312. }
  313. static grub_err_t
  314. erofs_map_blocks_chunkmode (grub_fshelp_node_t node,
  315. struct grub_erofs_map_blocks *map)
  316. {
  317. grub_uint16_t chunk_format = grub_le_to_cpu16 (node->inode.e.i_u.c.format);
  318. grub_uint64_t unit, pos, chunknr, blkaddr;
  319. grub_uint8_t chunkbits;
  320. grub_err_t err;
  321. if (chunk_format & EROFS_CHUNK_FORMAT_INDEXES)
  322. unit = sizeof (struct grub_erofs_inode_chunk_index);
  323. else
  324. unit = EROFS_BLOCK_MAP_ENTRY_SIZE;
  325. chunkbits = node->data->sb.log2_blksz + (chunk_format & EROFS_CHUNK_FORMAT_BLKBITS_MASK);
  326. if (chunkbits > 63)
  327. return grub_error (GRUB_ERR_BAD_FS, "invalid chunkbits %u @ inode %" PRIuGRUB_UINT64_T,
  328. chunkbits, node->ino);
  329. chunknr = map->m_la >> chunkbits;
  330. if (grub_add (erofs_iloc (node), erofs_inode_size (node), &pos))
  331. return grub_error (GRUB_ERR_OUT_OF_RANGE, "chunkmap position overflow when adding inode size");
  332. if (grub_add (pos, erofs_inode_xattr_ibody_size (node), &pos))
  333. return grub_error (GRUB_ERR_OUT_OF_RANGE, "chunkmap position overflow when adding xattr size");
  334. if (ALIGN_UP_OVF (pos, unit, &pos))
  335. return grub_error (GRUB_ERR_OUT_OF_RANGE, "position overflow when seeking at the start of chunkmap");
  336. /* No overflow for multiplication as chunkbits >= 9 and sizeof(unit) <= 8. */
  337. if (grub_add (pos, chunknr * unit, &pos))
  338. return grub_error (GRUB_ERR_OUT_OF_RANGE, "chunkmap position overflow when finding the specific chunk");
  339. map->m_la = chunknr << chunkbits;
  340. if (grub_sub (erofs_inode_file_size (node), map->m_la, &map->m_plen))
  341. return grub_error (GRUB_ERR_OUT_OF_RANGE, "m_plen underflow");
  342. map->m_plen = grub_min (((grub_uint64_t) 1) << chunkbits,
  343. ALIGN_UP (map->m_plen, erofs_blocksz (node->data)));
  344. if (chunk_format & EROFS_CHUNK_FORMAT_INDEXES)
  345. {
  346. struct grub_erofs_inode_chunk_index idx;
  347. err = grub_disk_read (node->data->disk, pos >> GRUB_DISK_SECTOR_BITS,
  348. pos & (GRUB_DISK_SECTOR_SIZE - 1), unit, &idx);
  349. if (err != GRUB_ERR_NONE)
  350. return err;
  351. blkaddr = grub_le_to_cpu32 (idx.blkaddr);
  352. }
  353. else
  354. {
  355. grub_uint32_t blkaddr_le;
  356. err = grub_disk_read (node->data->disk, pos >> GRUB_DISK_SECTOR_BITS,
  357. pos & (GRUB_DISK_SECTOR_SIZE - 1), unit, &blkaddr_le);
  358. if (err != GRUB_ERR_NONE)
  359. return err;
  360. blkaddr = grub_le_to_cpu32 (blkaddr_le);
  361. }
  362. if (blkaddr == EROFS_NULL_ADDR)
  363. {
  364. map->m_pa = 0;
  365. map->m_flags = 0;
  366. }
  367. else
  368. {
  369. map->m_pa = blkaddr << node->data->sb.log2_blksz;
  370. map->m_flags = EROFS_MAP_MAPPED;
  371. }
  372. map->m_llen = map->m_plen;
  373. return GRUB_ERR_NONE;
  374. }
  375. static grub_err_t
  376. erofs_map_blocks (grub_fshelp_node_t node, struct grub_erofs_map_blocks *map)
  377. {
  378. if (map->m_la >= erofs_inode_file_size (node))
  379. {
  380. map->m_llen = map->m_plen = 0;
  381. map->m_pa = 0;
  382. map->m_flags = 0;
  383. return GRUB_ERR_NONE;
  384. }
  385. if (node->inode_datalayout != EROFS_INODE_CHUNK_BASED)
  386. return erofs_map_blocks_flatmode (node, map);
  387. else
  388. return erofs_map_blocks_chunkmode (node, map);
  389. }
  390. static grub_err_t
  391. erofs_read_raw_data (grub_fshelp_node_t node, grub_uint8_t *buf, grub_uint64_t size,
  392. grub_uint64_t offset, grub_uint64_t *bytes)
  393. {
  394. struct grub_erofs_map_blocks map = {0};
  395. grub_uint64_t cur;
  396. grub_err_t err;
  397. if (bytes)
  398. *bytes = 0;
  399. if (node->inode_loaded == false)
  400. {
  401. err = erofs_read_inode (node->data, node);
  402. if (err != GRUB_ERR_NONE)
  403. return err;
  404. }
  405. cur = offset;
  406. while (cur < offset + size)
  407. {
  408. grub_uint8_t *const estart = buf + cur - offset;
  409. grub_uint64_t eend, moff = 0;
  410. map.m_la = cur;
  411. err = erofs_map_blocks (node, &map);
  412. if (err != GRUB_ERR_NONE)
  413. return err;
  414. if (grub_add(map.m_la, map.m_llen, &eend))
  415. return grub_error (GRUB_ERR_OUT_OF_RANGE, "eend overflow");
  416. eend = grub_min (eend, offset + size);
  417. if (!(map.m_flags & EROFS_MAP_MAPPED))
  418. {
  419. if (!map.m_llen)
  420. {
  421. /* Reached EOF. */
  422. grub_memset (estart, 0, offset + size - cur);
  423. cur = offset + size;
  424. continue;
  425. }
  426. /* It's a hole. */
  427. grub_memset (estart, 0, eend - cur);
  428. if (bytes)
  429. *bytes += eend - cur;
  430. cur = eend;
  431. continue;
  432. }
  433. if (cur > map.m_la)
  434. {
  435. moff = cur - map.m_la;
  436. map.m_la = cur;
  437. }
  438. err = grub_disk_read (node->data->disk,
  439. (map.m_pa + moff) >> GRUB_DISK_SECTOR_BITS,
  440. (map.m_pa + moff) & (GRUB_DISK_SECTOR_SIZE - 1),
  441. eend - map.m_la, estart);
  442. if (err != GRUB_ERR_NONE)
  443. return err;
  444. if (bytes)
  445. *bytes += eend - map.m_la;
  446. cur = eend;
  447. }
  448. return GRUB_ERR_NONE;
  449. }
  450. static int
  451. erofs_iterate_dir (grub_fshelp_node_t dir, grub_fshelp_iterate_dir_hook_t hook,
  452. void *hook_data)
  453. {
  454. grub_uint64_t offset = 0, file_size;
  455. grub_uint32_t blocksz = erofs_blocksz (dir->data);
  456. grub_uint8_t *buf;
  457. grub_err_t err;
  458. if (dir->inode_loaded == false)
  459. {
  460. err = erofs_read_inode (dir->data, dir);
  461. if (err != GRUB_ERR_NONE)
  462. return 0;
  463. }
  464. file_size = erofs_inode_file_size (dir);
  465. buf = grub_malloc (blocksz);
  466. if (buf == NULL)
  467. return 0;
  468. while (offset < file_size)
  469. {
  470. grub_uint64_t maxsize = grub_min (blocksz, file_size - offset);
  471. struct grub_erofs_dirent *de = (void *) buf, *end;
  472. grub_uint16_t nameoff;
  473. err = erofs_read_raw_data (dir, buf, maxsize, offset, NULL);
  474. if (err != GRUB_ERR_NONE)
  475. goto not_found;
  476. nameoff = grub_le_to_cpu16 (de->nameoff);
  477. if (nameoff < sizeof (struct grub_erofs_dirent) || nameoff >= maxsize)
  478. {
  479. grub_error (GRUB_ERR_BAD_FS,
  480. "invalid nameoff %u @ inode %" PRIuGRUB_UINT64_T,
  481. nameoff, dir->ino);
  482. goto not_found;
  483. }
  484. end = (struct grub_erofs_dirent *) ((grub_uint8_t *) de + nameoff);
  485. while (de < end)
  486. {
  487. struct grub_fshelp_node *fdiro;
  488. enum grub_fshelp_filetype type;
  489. char filename[EROFS_NAME_LEN + 1];
  490. grub_size_t de_namelen;
  491. const char *de_name;
  492. fdiro = grub_malloc (sizeof (struct grub_fshelp_node));
  493. if (fdiro == NULL)
  494. goto not_found;
  495. fdiro->data = dir->data;
  496. fdiro->ino = grub_le_to_cpu64 (de->nid);
  497. fdiro->inode_loaded = false;
  498. nameoff = grub_le_to_cpu16 (de->nameoff);
  499. if (nameoff < sizeof (struct grub_erofs_dirent) || nameoff >= maxsize)
  500. {
  501. grub_error (GRUB_ERR_BAD_FS,
  502. "invalid nameoff %u @ inode %" PRIuGRUB_UINT64_T,
  503. nameoff, dir->ino);
  504. grub_free (fdiro);
  505. goto not_found;
  506. }
  507. de_name = (char *) buf + nameoff;
  508. if (de + 1 >= end)
  509. de_namelen = grub_erofs_strnlen (de_name, maxsize - nameoff);
  510. else
  511. {
  512. if (grub_sub (grub_le_to_cpu16 (de[1].nameoff), nameoff, &de_namelen))
  513. {
  514. grub_error (GRUB_ERR_OUT_OF_RANGE, "de_namelen underflow");
  515. grub_free (fdiro);
  516. goto not_found;
  517. }
  518. }
  519. if (nameoff + de_namelen > maxsize || de_namelen > EROFS_NAME_LEN)
  520. {
  521. grub_error (GRUB_ERR_BAD_FS,
  522. "invalid de_namelen %" PRIuGRUB_SIZE
  523. " @ inode %" PRIuGRUB_UINT64_T,
  524. de_namelen, dir->ino);
  525. grub_free (fdiro);
  526. goto not_found;
  527. }
  528. grub_memcpy (filename, de_name, de_namelen);
  529. filename[de_namelen] = '\0';
  530. switch (grub_le_to_cpu16 (de->file_type))
  531. {
  532. case EROFS_FT_REG_FILE:
  533. case EROFS_FT_BLKDEV:
  534. case EROFS_FT_CHRDEV:
  535. case EROFS_FT_FIFO:
  536. case EROFS_FT_SOCK:
  537. type = GRUB_FSHELP_REG;
  538. break;
  539. case EROFS_FT_DIR:
  540. type = GRUB_FSHELP_DIR;
  541. break;
  542. case EROFS_FT_SYMLINK:
  543. type = GRUB_FSHELP_SYMLINK;
  544. break;
  545. case EROFS_FT_UNKNOWN:
  546. default:
  547. type = GRUB_FSHELP_UNKNOWN;
  548. }
  549. if (hook (filename, type, fdiro, hook_data))
  550. {
  551. grub_free (buf);
  552. return 1;
  553. }
  554. ++de;
  555. }
  556. offset += maxsize;
  557. }
  558. not_found:
  559. grub_free (buf);
  560. return 0;
  561. }
  562. static char *
  563. erofs_read_symlink (grub_fshelp_node_t node)
  564. {
  565. char *symlink;
  566. grub_size_t sz;
  567. grub_err_t err;
  568. if (node->inode_loaded == false)
  569. {
  570. err = erofs_read_inode (node->data, node);
  571. if (err != GRUB_ERR_NONE)
  572. return NULL;
  573. }
  574. sz = erofs_inode_file_size (node);
  575. if (sz >= EROFS_PATH_LEN)
  576. {
  577. grub_error (GRUB_ERR_BAD_FS,
  578. "symlink too long @ inode %" PRIuGRUB_UINT64_T, node->ino);
  579. return NULL;
  580. }
  581. symlink = grub_malloc (sz + 1);
  582. if (symlink == NULL)
  583. return NULL;
  584. err = erofs_read_raw_data (node, (grub_uint8_t *) symlink, sz, 0, NULL);
  585. if (err != GRUB_ERR_NONE)
  586. {
  587. grub_free (symlink);
  588. return NULL;
  589. }
  590. symlink[sz] = '\0';
  591. return symlink;
  592. }
  593. static struct grub_erofs_data *
  594. erofs_mount (grub_disk_t disk, bool read_root)
  595. {
  596. struct grub_erofs_super sb;
  597. grub_err_t err;
  598. struct grub_erofs_data *data;
  599. grub_uint32_t feature;
  600. err = grub_disk_read (disk, EROFS_SUPER_OFFSET >> GRUB_DISK_SECTOR_BITS, 0,
  601. sizeof (sb), &sb);
  602. if (err != GRUB_ERR_NONE)
  603. return NULL;
  604. if (sb.magic != grub_cpu_to_le32_compile_time (EROFS_MAGIC) ||
  605. grub_le_to_cpu32 (sb.log2_blksz) < EROFS_MIN_LOG2_BLOCK_SIZE ||
  606. grub_le_to_cpu32 (sb.log2_blksz) > EROFS_MAX_LOG2_BLOCK_SIZE)
  607. {
  608. grub_error (GRUB_ERR_BAD_FS, "not a valid erofs filesystem");
  609. return NULL;
  610. }
  611. feature = grub_le_to_cpu32 (sb.feature_incompat);
  612. if (feature & ~EROFS_ALL_FEATURE_INCOMPAT)
  613. {
  614. grub_error (GRUB_ERR_BAD_FS, "unsupported features: 0x%x",
  615. feature & ~EROFS_ALL_FEATURE_INCOMPAT);
  616. return NULL;
  617. }
  618. data = grub_malloc (sizeof (*data));
  619. if (data == NULL)
  620. return NULL;
  621. data->disk = disk;
  622. data->sb = sb;
  623. if (read_root)
  624. {
  625. data->inode.data = data;
  626. data->inode.ino = grub_le_to_cpu16 (sb.root_nid);
  627. err = erofs_read_inode (data, &data->inode);
  628. if (err != GRUB_ERR_NONE)
  629. {
  630. grub_free (data);
  631. return NULL;
  632. }
  633. }
  634. return data;
  635. }
  636. /* Context for grub_erofs_dir. */
  637. struct grub_erofs_dir_ctx
  638. {
  639. grub_fs_dir_hook_t hook;
  640. void *hook_data;
  641. struct grub_erofs_data *data;
  642. };
  643. /* Helper for grub_erofs_dir. */
  644. static int
  645. erofs_dir_iter (const char *filename, enum grub_fshelp_filetype filetype,
  646. grub_fshelp_node_t node, void *data)
  647. {
  648. struct grub_erofs_dir_ctx *ctx = data;
  649. struct grub_dirhook_info info = {0};
  650. grub_err_t err;
  651. if (node->inode_loaded == false)
  652. {
  653. err = erofs_read_inode (ctx->data, node);
  654. if (err != GRUB_ERR_NONE)
  655. return 0;
  656. }
  657. if (node->inode_loaded == true)
  658. {
  659. info.mtimeset = 1;
  660. info.mtime = erofs_inode_mtime (node);
  661. }
  662. info.dir = ((filetype & GRUB_FSHELP_TYPE_MASK) == GRUB_FSHELP_DIR);
  663. grub_free (node);
  664. return ctx->hook (filename, &info, ctx->hook_data);
  665. }
  666. static grub_err_t
  667. grub_erofs_dir (grub_device_t device, const char *path, grub_fs_dir_hook_t hook,
  668. void *hook_data)
  669. {
  670. grub_fshelp_node_t fdiro = NULL;
  671. grub_err_t err;
  672. struct grub_erofs_dir_ctx ctx = {
  673. .hook = hook,
  674. .hook_data = hook_data
  675. };
  676. ctx.data = erofs_mount (device->disk, true);
  677. if (ctx.data == NULL)
  678. goto fail;
  679. err = grub_fshelp_find_file (path, &ctx.data->inode, &fdiro, erofs_iterate_dir,
  680. erofs_read_symlink, GRUB_FSHELP_DIR);
  681. if (err != GRUB_ERR_NONE)
  682. goto fail;
  683. erofs_iterate_dir (fdiro, erofs_dir_iter, &ctx);
  684. fail:
  685. if (fdiro != &ctx.data->inode)
  686. grub_free (fdiro);
  687. grub_free (ctx.data);
  688. return grub_errno;
  689. }
  690. static grub_err_t
  691. grub_erofs_open (grub_file_t file, const char *name)
  692. {
  693. struct grub_erofs_data *data;
  694. struct grub_fshelp_node *fdiro = NULL;
  695. grub_err_t err;
  696. data = erofs_mount (file->device->disk, true);
  697. if (data == NULL)
  698. {
  699. err = grub_errno;
  700. goto fail;
  701. }
  702. err = grub_fshelp_find_file (name, &data->inode, &fdiro, erofs_iterate_dir,
  703. erofs_read_symlink, GRUB_FSHELP_REG);
  704. if (err != GRUB_ERR_NONE)
  705. goto fail;
  706. if (fdiro->inode_loaded == false)
  707. {
  708. err = erofs_read_inode (data, fdiro);
  709. if (err != GRUB_ERR_NONE)
  710. goto fail;
  711. }
  712. grub_memcpy (&data->inode, fdiro, sizeof (*fdiro));
  713. grub_free (fdiro);
  714. file->data = data;
  715. file->size = erofs_inode_file_size (&data->inode);
  716. return GRUB_ERR_NONE;
  717. fail:
  718. if (fdiro != &data->inode)
  719. grub_free (fdiro);
  720. grub_free (data);
  721. return err;
  722. }
  723. static grub_ssize_t
  724. grub_erofs_read (grub_file_t file, char *buf, grub_size_t len)
  725. {
  726. struct grub_erofs_data *data = file->data;
  727. struct grub_fshelp_node *inode = &data->inode;
  728. grub_off_t off = file->offset;
  729. grub_uint64_t ret = 0, file_size;
  730. grub_err_t err;
  731. if (inode->inode_loaded == false)
  732. {
  733. err = erofs_read_inode (data, inode);
  734. if (err != GRUB_ERR_NONE)
  735. return -1;
  736. }
  737. file_size = erofs_inode_file_size (inode);
  738. if (off > file_size)
  739. {
  740. grub_error (GRUB_ERR_IO, "read past EOF @ inode %" PRIuGRUB_UINT64_T, inode->ino);
  741. return -1;
  742. }
  743. if (off == file_size)
  744. return 0;
  745. if (off + len > file_size)
  746. len = file_size - off;
  747. err = erofs_read_raw_data (inode, (grub_uint8_t *) buf, len, off, &ret);
  748. if (err != GRUB_ERR_NONE)
  749. return -1;
  750. return ret;
  751. }
  752. static grub_err_t
  753. grub_erofs_close (grub_file_t file)
  754. {
  755. grub_free (file->data);
  756. return GRUB_ERR_NONE;
  757. }
  758. static grub_err_t
  759. grub_erofs_uuid (grub_device_t device, char **uuid)
  760. {
  761. struct grub_erofs_data *data;
  762. data = erofs_mount (device->disk, false);
  763. if (data == NULL)
  764. {
  765. *uuid = NULL;
  766. return grub_errno;
  767. }
  768. *uuid = grub_xasprintf ("%pG", &data->sb.uuid);
  769. grub_free (data);
  770. return GRUB_ERR_NONE;
  771. }
  772. static grub_err_t
  773. grub_erofs_label (grub_device_t device, char **label)
  774. {
  775. struct grub_erofs_data *data;
  776. data = erofs_mount (device->disk, false);
  777. if (data == NULL)
  778. {
  779. *label = NULL;
  780. return grub_errno;
  781. }
  782. *label = grub_strndup ((char *) data->sb.volume_name, sizeof (data->sb.volume_name));
  783. grub_free (data);
  784. if (*label == NULL)
  785. return grub_errno;
  786. return GRUB_ERR_NONE;
  787. }
  788. static grub_err_t
  789. grub_erofs_mtime (grub_device_t device, grub_int64_t *tm)
  790. {
  791. struct grub_erofs_data *data;
  792. data = erofs_mount (device->disk, false);
  793. if (data == NULL)
  794. {
  795. *tm = 0;
  796. return grub_errno;
  797. }
  798. *tm = grub_le_to_cpu64 (data->sb.build_time);
  799. grub_free (data);
  800. return GRUB_ERR_NONE;
  801. }
  802. static struct grub_fs grub_erofs_fs = {
  803. .name = "erofs",
  804. .fs_dir = grub_erofs_dir,
  805. .fs_open = grub_erofs_open,
  806. .fs_read = grub_erofs_read,
  807. .fs_close = grub_erofs_close,
  808. .fs_uuid = grub_erofs_uuid,
  809. .fs_label = grub_erofs_label,
  810. .fs_mtime = grub_erofs_mtime,
  811. #ifdef GRUB_UTIL
  812. .reserved_first_sector = 1,
  813. .blocklist_install = 0,
  814. #endif
  815. .next = 0,
  816. };
  817. GRUB_MOD_INIT (erofs)
  818. {
  819. grub_fs_register (&grub_erofs_fs);
  820. }
  821. GRUB_MOD_FINI (erofs)
  822. {
  823. grub_fs_unregister (&grub_erofs_fs);
  824. }