jfs.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021
  1. /* jfs.c - JFS. */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2004,2005,2006,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/err.h>
  20. #include <grub/file.h>
  21. #include <grub/mm.h>
  22. #include <grub/misc.h>
  23. #include <grub/disk.h>
  24. #include <grub/dl.h>
  25. #include <grub/types.h>
  26. #include <grub/charset.h>
  27. #include <grub/i18n.h>
  28. #include <grub/lockdown.h>
  29. GRUB_MOD_LICENSE ("GPLv3+");
  30. #define GRUB_JFS_MAX_SYMLNK_CNT 8
  31. #define GRUB_JFS_FILETYPE_MASK 0170000
  32. #define GRUB_JFS_FILETYPE_REG 0100000
  33. #define GRUB_JFS_FILETYPE_LNK 0120000
  34. #define GRUB_JFS_FILETYPE_DIR 0040000
  35. #define GRUB_JFS_SBLOCK 64
  36. #define GRUB_JFS_AGGR_INODE 2
  37. #define GRUB_JFS_FS1_INODE_BLK 104
  38. #define GRUB_JFS_TREE_LEAF 2
  39. /*
  40. * Define max entries stored in-line in an inode.
  41. * https://jfs.sourceforge.net/project/pub/jfslayout.pdf
  42. */
  43. #define GRUB_JFS_INODE_INLINE_ENTRIES 8
  44. #define GRUB_JFS_DIR_MAX_SLOTS 128
  45. struct grub_jfs_sblock
  46. {
  47. /* The magic for JFS. It should contain the string "JFS1". */
  48. grub_uint8_t magic[4];
  49. grub_uint32_t version;
  50. grub_uint64_t ag_size;
  51. /* The size of a filesystem block in bytes. XXX: currently only
  52. 4096 was tested. */
  53. grub_uint32_t blksz;
  54. grub_uint16_t log2_blksz;
  55. grub_uint8_t unused[14];
  56. grub_uint32_t flags;
  57. grub_uint8_t unused3[61];
  58. char volname[11];
  59. grub_uint8_t unused2[24];
  60. grub_uint8_t uuid[16];
  61. char volname2[16];
  62. };
  63. struct grub_jfs_extent
  64. {
  65. /* The length of the extent in filesystem blocks. */
  66. grub_uint16_t length;
  67. grub_uint8_t length2;
  68. /* The physical offset of the first block on the disk. */
  69. grub_uint8_t blk1;
  70. grub_uint32_t blk2;
  71. } GRUB_PACKED;
  72. #define GRUB_JFS_IAG_INODES_OFFSET 3072
  73. #define GRUB_JFS_IAG_INODES_COUNT 128
  74. struct grub_jfs_iag
  75. {
  76. grub_uint8_t unused[GRUB_JFS_IAG_INODES_OFFSET];
  77. struct grub_jfs_extent inodes[GRUB_JFS_IAG_INODES_COUNT];
  78. } GRUB_PACKED;
  79. /* The head of the tree used to find extents. */
  80. struct grub_jfs_treehead
  81. {
  82. grub_uint64_t next;
  83. grub_uint64_t prev;
  84. grub_uint8_t flags;
  85. grub_uint8_t unused;
  86. grub_uint16_t count;
  87. grub_uint16_t max;
  88. grub_uint8_t unused2[10];
  89. } GRUB_PACKED;
  90. /* A node in the extent tree. */
  91. struct grub_jfs_tree_extent
  92. {
  93. grub_uint8_t flags;
  94. grub_uint16_t unused;
  95. /* The offset is the key used to lookup an extent. */
  96. grub_uint8_t offset1;
  97. grub_uint32_t offset2;
  98. struct grub_jfs_extent extent;
  99. } GRUB_PACKED;
  100. /* The tree of directory entries. */
  101. struct grub_jfs_tree_dir
  102. {
  103. /* Pointers to the previous and next tree headers of other nodes on
  104. this level. */
  105. grub_uint64_t nextb;
  106. grub_uint64_t prevb;
  107. grub_uint8_t flags;
  108. /* The amount of dirents in this node. */
  109. grub_uint8_t count;
  110. grub_uint8_t freecnt;
  111. grub_uint8_t freelist;
  112. grub_uint8_t maxslot;
  113. /* The location of the sorted array of pointers to dirents. */
  114. grub_uint8_t sindex;
  115. grub_uint8_t unused[10];
  116. } GRUB_PACKED;
  117. /* An internal node in the dirents tree. */
  118. struct grub_jfs_internal_dirent
  119. {
  120. struct grub_jfs_extent ex;
  121. grub_uint8_t next;
  122. grub_uint8_t len;
  123. grub_uint16_t namepart[11];
  124. } GRUB_PACKED;
  125. /* A leaf node in the dirents tree. */
  126. struct grub_jfs_leaf_dirent
  127. {
  128. /* The inode for this dirent. */
  129. grub_uint32_t inode;
  130. grub_uint8_t next;
  131. /* The size of the name. */
  132. grub_uint8_t len;
  133. grub_uint16_t namepart[11];
  134. grub_uint32_t index;
  135. } GRUB_PACKED;
  136. /* A leaf in the dirents tree. This one is used if the previously
  137. dirent was not big enough to store the name. */
  138. struct grub_jfs_leaf_next_dirent
  139. {
  140. grub_uint8_t next;
  141. grub_uint8_t len;
  142. grub_uint16_t namepart[15];
  143. } GRUB_PACKED;
  144. struct grub_jfs_time
  145. {
  146. grub_int32_t sec;
  147. grub_int32_t nanosec;
  148. } GRUB_PACKED;
  149. struct grub_jfs_inode
  150. {
  151. grub_uint32_t stamp;
  152. grub_uint32_t fileset;
  153. grub_uint32_t inode;
  154. grub_uint8_t unused[12];
  155. grub_uint64_t size;
  156. grub_uint8_t unused2[20];
  157. grub_uint32_t mode;
  158. struct grub_jfs_time atime;
  159. struct grub_jfs_time ctime;
  160. struct grub_jfs_time mtime;
  161. grub_uint8_t unused3[48];
  162. grub_uint8_t unused4[96];
  163. union
  164. {
  165. /* The tree describing the extents of the file. */
  166. struct GRUB_PACKED
  167. {
  168. struct grub_jfs_treehead tree;
  169. struct grub_jfs_tree_extent extents[16];
  170. } file;
  171. union
  172. {
  173. /* The tree describing the dirents. */
  174. struct
  175. {
  176. grub_uint8_t unused[16];
  177. grub_uint8_t flags;
  178. /* Amount of dirents in this node. */
  179. grub_uint8_t count;
  180. grub_uint8_t freecnt;
  181. grub_uint8_t freelist;
  182. grub_uint32_t idotdot;
  183. grub_uint8_t sorted[GRUB_JFS_INODE_INLINE_ENTRIES];
  184. } header;
  185. struct grub_jfs_leaf_dirent dirents[GRUB_JFS_INODE_INLINE_ENTRIES];
  186. } GRUB_PACKED dir;
  187. /* Fast symlink. */
  188. struct
  189. {
  190. grub_uint8_t unused[32];
  191. grub_uint8_t path[256];
  192. } symlink;
  193. } GRUB_PACKED;
  194. } GRUB_PACKED;
  195. struct grub_jfs_data
  196. {
  197. struct grub_jfs_sblock sblock;
  198. grub_disk_t disk;
  199. struct grub_jfs_inode fileset;
  200. struct grub_jfs_inode currinode;
  201. int caseins;
  202. int pos;
  203. int linknest;
  204. int namecomponentlen;
  205. } GRUB_PACKED;
  206. struct grub_jfs_diropen
  207. {
  208. int index;
  209. union
  210. {
  211. struct grub_jfs_tree_dir header;
  212. struct grub_jfs_leaf_dirent dirent[0];
  213. struct grub_jfs_leaf_next_dirent next_dirent[0];
  214. grub_uint8_t sorted[0];
  215. } GRUB_PACKED *dirpage;
  216. struct grub_jfs_data *data;
  217. struct grub_jfs_inode *inode;
  218. int count;
  219. grub_uint8_t *sorted;
  220. struct grub_jfs_leaf_dirent *leaf;
  221. struct grub_jfs_leaf_next_dirent *next_leaf;
  222. /* The filename and inode of the last read dirent. */
  223. /* On-disk name is at most 255 UTF-16 codepoints.
  224. Every UTF-16 codepoint is at most 4 UTF-8 bytes.
  225. */
  226. char name[256 * GRUB_MAX_UTF8_PER_UTF16 + 1];
  227. grub_uint32_t ino;
  228. } GRUB_PACKED;
  229. static grub_dl_t my_mod;
  230. static grub_err_t grub_jfs_lookup_symlink (struct grub_jfs_data *data, grub_uint32_t ino);
  231. /*
  232. * An extent's offset, physical and logical, is represented as a 40-bit value.
  233. * This 40-bit value is split into two parts:
  234. * - offset1: the most signficant 8 bits of the offset,
  235. * - offset2: the least significant 32 bits of the offset.
  236. *
  237. * This function calculates and returns the 64-bit offset of an extent.
  238. */
  239. static grub_uint64_t
  240. get_ext_offset (grub_uint8_t offset1, grub_uint32_t offset2)
  241. {
  242. return (((grub_uint64_t) offset1 << 32) | grub_le_to_cpu32 (offset2));
  243. }
  244. static grub_uint64_t
  245. getblk (struct grub_jfs_treehead *treehead,
  246. struct grub_jfs_tree_extent *extents,
  247. int max_extents,
  248. struct grub_jfs_data *data,
  249. grub_uint64_t blk)
  250. {
  251. int found = -1;
  252. int i;
  253. grub_uint64_t ext_offset, ext_blk;
  254. grub_errno = GRUB_ERR_NONE;
  255. for (i = 0; i < grub_le_to_cpu16 (treehead->count) - 2 &&
  256. i < max_extents; i++)
  257. {
  258. ext_offset = get_ext_offset (extents[i].offset1, extents[i].offset2);
  259. ext_blk = get_ext_offset (extents[i].extent.blk1, extents[i].extent.blk2);
  260. if (treehead->flags & GRUB_JFS_TREE_LEAF)
  261. {
  262. /* Read the leafnode. */
  263. if (ext_offset <= blk
  264. && ((grub_le_to_cpu16 (extents[i].extent.length))
  265. + (extents[i].extent.length2 << 16)
  266. + ext_offset) > blk)
  267. return (blk - ext_offset + ext_blk);
  268. }
  269. else
  270. if (blk >= ext_offset)
  271. found = i;
  272. }
  273. if (found != -1)
  274. {
  275. grub_uint64_t ret = 0;
  276. struct
  277. {
  278. struct grub_jfs_treehead treehead;
  279. struct grub_jfs_tree_extent extents[254];
  280. } *tree;
  281. tree = grub_zalloc (sizeof (*tree));
  282. if (!tree)
  283. return 0;
  284. if (!grub_disk_read (data->disk,
  285. (grub_disk_addr_t) ext_blk
  286. << (grub_le_to_cpu16 (data->sblock.log2_blksz) - GRUB_DISK_SECTOR_BITS),
  287. 0, sizeof (*tree), (char *) tree))
  288. {
  289. if (grub_memcmp (&tree->treehead, treehead, sizeof (struct grub_jfs_treehead)) ||
  290. grub_memcmp (&tree->extents, extents, 254 * sizeof (struct grub_jfs_tree_extent)))
  291. ret = getblk (&tree->treehead, &tree->extents[0], 254, data, blk);
  292. else
  293. {
  294. grub_error (GRUB_ERR_BAD_FS, "jfs: infinite recursion detected");
  295. ret = 0;
  296. }
  297. }
  298. grub_free (tree);
  299. return ret;
  300. }
  301. grub_error (GRUB_ERR_READ_ERROR, "jfs: block %" PRIuGRUB_UINT64_T " not found", blk);
  302. return 0;
  303. }
  304. /* Get the block number for the block BLK in the node INODE in the
  305. mounted filesystem DATA. */
  306. static grub_uint64_t
  307. grub_jfs_blkno (struct grub_jfs_data *data, struct grub_jfs_inode *inode,
  308. grub_uint64_t blk)
  309. {
  310. return getblk (&inode->file.tree, &inode->file.extents[0], 16, data, blk);
  311. }
  312. static grub_err_t
  313. grub_jfs_read_inode (struct grub_jfs_data *data, grub_uint32_t ino,
  314. struct grub_jfs_inode *inode)
  315. {
  316. struct grub_jfs_extent iag_inodes[GRUB_JFS_IAG_INODES_COUNT];
  317. grub_uint32_t iagnum = ino / 4096;
  318. unsigned inoext = (ino % 4096) / 32;
  319. unsigned inonum = (ino % 4096) % 32;
  320. grub_uint64_t iagblk;
  321. grub_uint64_t inoblk;
  322. iagblk = grub_jfs_blkno (data, &data->fileset, iagnum + 1);
  323. if (grub_errno)
  324. return grub_errno;
  325. /* Read in the IAG. */
  326. if (grub_disk_read (data->disk,
  327. iagblk << (grub_le_to_cpu16 (data->sblock.log2_blksz)
  328. - GRUB_DISK_SECTOR_BITS),
  329. GRUB_JFS_IAG_INODES_OFFSET,
  330. sizeof (iag_inodes), &iag_inodes))
  331. return grub_errno;
  332. inoblk = get_ext_offset (iag_inodes[inoext].blk1, iag_inodes[inoext].blk2);
  333. inoblk <<= (grub_le_to_cpu16 (data->sblock.log2_blksz)
  334. - GRUB_DISK_SECTOR_BITS);
  335. inoblk += inonum;
  336. if (grub_disk_read (data->disk, inoblk, 0,
  337. sizeof (struct grub_jfs_inode), inode))
  338. return grub_errno;
  339. return 0;
  340. }
  341. static struct grub_jfs_data *
  342. grub_jfs_mount (grub_disk_t disk)
  343. {
  344. struct grub_jfs_data *data = 0;
  345. data = grub_malloc (sizeof (struct grub_jfs_data));
  346. if (!data)
  347. return 0;
  348. /* Read the superblock. */
  349. if (grub_disk_read (disk, GRUB_JFS_SBLOCK, 0,
  350. sizeof (struct grub_jfs_sblock), &data->sblock))
  351. goto fail;
  352. if (grub_strncmp ((char *) (data->sblock.magic), "JFS1", 4))
  353. {
  354. grub_error (GRUB_ERR_BAD_FS, "not a JFS filesystem");
  355. goto fail;
  356. }
  357. if (data->sblock.blksz == 0
  358. || grub_le_to_cpu32 (data->sblock.blksz)
  359. != (1U << grub_le_to_cpu16 (data->sblock.log2_blksz))
  360. || grub_le_to_cpu16 (data->sblock.log2_blksz) < GRUB_DISK_SECTOR_BITS)
  361. {
  362. grub_error (GRUB_ERR_BAD_FS, "not a JFS filesystem");
  363. goto fail;
  364. }
  365. data->disk = disk;
  366. data->pos = 0;
  367. data->linknest = 0;
  368. /* Read the inode of the first fileset. */
  369. if (grub_disk_read (data->disk, GRUB_JFS_FS1_INODE_BLK, 0,
  370. sizeof (struct grub_jfs_inode), &data->fileset))
  371. goto fail;
  372. if (data->sblock.flags & grub_cpu_to_le32_compile_time (0x00200000))
  373. data->namecomponentlen = 11;
  374. else
  375. data->namecomponentlen = 13;
  376. if (data->sblock.flags & grub_cpu_to_le32_compile_time (0x40000000))
  377. data->caseins = 1;
  378. else
  379. data->caseins = 0;
  380. return data;
  381. fail:
  382. grub_free (data);
  383. if (grub_errno == GRUB_ERR_OUT_OF_RANGE)
  384. grub_error (GRUB_ERR_BAD_FS, "not a JFS filesystem");
  385. return 0;
  386. }
  387. static struct grub_jfs_diropen *
  388. grub_jfs_opendir (struct grub_jfs_data *data, struct grub_jfs_inode *inode)
  389. {
  390. struct grub_jfs_internal_dirent *de;
  391. struct grub_jfs_diropen *diro;
  392. grub_disk_addr_t blk;
  393. de = (struct grub_jfs_internal_dirent *) inode->dir.dirents;
  394. if (!((grub_le_to_cpu32 (inode->mode)
  395. & GRUB_JFS_FILETYPE_MASK) == GRUB_JFS_FILETYPE_DIR))
  396. {
  397. grub_error (GRUB_ERR_BAD_FILE_TYPE, N_("not a directory"));
  398. return 0;
  399. }
  400. diro = grub_zalloc (sizeof (struct grub_jfs_diropen));
  401. if (!diro)
  402. return 0;
  403. diro->data = data;
  404. diro->inode = inode;
  405. /* Check if the entire tree is contained within the inode. */
  406. if (inode->file.tree.flags & GRUB_JFS_TREE_LEAF)
  407. {
  408. if (inode->dir.header.count > GRUB_JFS_INODE_INLINE_ENTRIES)
  409. {
  410. grub_free (diro);
  411. grub_error (GRUB_ERR_BAD_FS, N_("invalid JFS inode"));
  412. return 0;
  413. }
  414. diro->leaf = inode->dir.dirents;
  415. diro->next_leaf = (struct grub_jfs_leaf_next_dirent *) de;
  416. diro->sorted = inode->dir.header.sorted;
  417. diro->count = inode->dir.header.count;
  418. return diro;
  419. }
  420. diro->dirpage = grub_malloc (grub_le_to_cpu32 (data->sblock.blksz));
  421. if (!diro->dirpage)
  422. {
  423. grub_free (diro);
  424. return 0;
  425. }
  426. if (inode->dir.header.sorted[0] >= GRUB_JFS_DIR_MAX_SLOTS)
  427. {
  428. grub_error (GRUB_ERR_BAD_FS, N_("invalid directory slot index"));
  429. grub_free (diro->dirpage);
  430. grub_free (diro);
  431. return 0;
  432. }
  433. blk = get_ext_offset (de[inode->dir.header.sorted[0]].ex.blk1,
  434. de[inode->dir.header.sorted[0]].ex.blk2);
  435. blk <<= (grub_le_to_cpu16 (data->sblock.log2_blksz) - GRUB_DISK_SECTOR_BITS);
  436. /* Read in the nodes until we are on the leaf node level. */
  437. do
  438. {
  439. int index;
  440. if (grub_disk_read (data->disk, blk, 0,
  441. grub_le_to_cpu32 (data->sblock.blksz),
  442. diro->dirpage->sorted))
  443. {
  444. grub_free (diro->dirpage);
  445. grub_free (diro);
  446. return 0;
  447. }
  448. de = (struct grub_jfs_internal_dirent *) diro->dirpage->dirent;
  449. index = diro->dirpage->sorted[diro->dirpage->header.sindex * 32];
  450. blk = (get_ext_offset (de[index].ex.blk1, de[index].ex.blk2)
  451. << (grub_le_to_cpu16 (data->sblock.log2_blksz)
  452. - GRUB_DISK_SECTOR_BITS));
  453. } while (!(diro->dirpage->header.flags & GRUB_JFS_TREE_LEAF));
  454. diro->leaf = diro->dirpage->dirent;
  455. diro->next_leaf = diro->dirpage->next_dirent;
  456. diro->sorted = &diro->dirpage->sorted[diro->dirpage->header.sindex * 32];
  457. diro->count = diro->dirpage->header.count;
  458. return diro;
  459. }
  460. static void
  461. grub_jfs_closedir (struct grub_jfs_diropen *diro)
  462. {
  463. if (!diro)
  464. return;
  465. grub_free (diro->dirpage);
  466. grub_free (diro);
  467. }
  468. static void
  469. le_to_cpu16_copy (grub_uint16_t *out, grub_uint16_t *in, grub_size_t len)
  470. {
  471. while (len--)
  472. *out++ = grub_le_to_cpu16 (*in++);
  473. }
  474. #if __GNUC__ >= 9
  475. #pragma GCC diagnostic push
  476. #pragma GCC diagnostic ignored "-Waddress-of-packed-member"
  477. #endif
  478. /* Read in the next dirent from the directory described by DIRO. */
  479. static grub_err_t
  480. grub_jfs_getent (struct grub_jfs_diropen *diro)
  481. {
  482. int strpos = 0;
  483. struct grub_jfs_leaf_dirent *leaf;
  484. struct grub_jfs_leaf_next_dirent *next_leaf;
  485. int len;
  486. int nextent;
  487. grub_uint16_t filename[256];
  488. /* The last node, read in more. */
  489. if (diro->index == diro->count)
  490. {
  491. grub_disk_addr_t next;
  492. /* If the inode contains the entry tree or if this was the last
  493. node, there is nothing to read. */
  494. if ((diro->inode->file.tree.flags & GRUB_JFS_TREE_LEAF)
  495. || !grub_le_to_cpu64 (diro->dirpage->header.nextb))
  496. return GRUB_ERR_OUT_OF_RANGE;
  497. next = grub_le_to_cpu64 (diro->dirpage->header.nextb);
  498. next <<= (grub_le_to_cpu16 (diro->data->sblock.log2_blksz)
  499. - GRUB_DISK_SECTOR_BITS);
  500. if (grub_disk_read (diro->data->disk, next, 0,
  501. grub_le_to_cpu32 (diro->data->sblock.blksz),
  502. diro->dirpage->sorted))
  503. return grub_errno;
  504. diro->leaf = diro->dirpage->dirent;
  505. diro->next_leaf = diro->dirpage->next_dirent;
  506. diro->sorted = &diro->dirpage->sorted[diro->dirpage->header.sindex * 32];
  507. diro->count = diro->dirpage->header.count;
  508. diro->index = 0;
  509. }
  510. leaf = &diro->leaf[diro->sorted[diro->index]];
  511. next_leaf = &diro->next_leaf[diro->index];
  512. len = leaf->len;
  513. if (!len)
  514. {
  515. diro->index++;
  516. return grub_jfs_getent (diro);
  517. }
  518. le_to_cpu16_copy (filename + strpos, leaf->namepart, len < diro->data->namecomponentlen ? len
  519. : diro->data->namecomponentlen);
  520. strpos += len < diro->data->namecomponentlen ? len
  521. : diro->data->namecomponentlen;
  522. diro->ino = grub_le_to_cpu32 (leaf->inode);
  523. len -= diro->data->namecomponentlen;
  524. /* Move down to the leaf level. */
  525. nextent = leaf->next;
  526. if (leaf->next != 255 && len > 0)
  527. do
  528. {
  529. next_leaf = &diro->next_leaf[nextent];
  530. le_to_cpu16_copy (filename + strpos, next_leaf->namepart, len < 15 ? len : 15);
  531. strpos += len < 15 ? len : 15;
  532. len -= 15;
  533. nextent = next_leaf->next;
  534. } while (next_leaf->next != 255 && len > 0);
  535. diro->index++;
  536. /* Convert the temporary UTF16 filename to UTF8. */
  537. *grub_utf16_to_utf8 ((grub_uint8_t *) (diro->name), filename, strpos) = '\0';
  538. return 0;
  539. }
  540. #if __GNUC__ >= 9
  541. #pragma GCC diagnostic pop
  542. #endif
  543. /* Read LEN bytes from the file described by DATA starting with byte
  544. POS. Return the amount of read bytes in READ. */
  545. static grub_ssize_t
  546. grub_jfs_read_file (struct grub_jfs_data *data,
  547. grub_disk_read_hook_t read_hook, void *read_hook_data,
  548. grub_off_t pos, grub_size_t len, char *buf)
  549. {
  550. grub_off_t i;
  551. grub_off_t blockcnt;
  552. blockcnt = (len + pos + grub_le_to_cpu32 (data->sblock.blksz) - 1)
  553. >> grub_le_to_cpu16 (data->sblock.log2_blksz);
  554. for (i = pos >> grub_le_to_cpu16 (data->sblock.log2_blksz); i < blockcnt; i++)
  555. {
  556. grub_disk_addr_t blknr;
  557. grub_uint32_t blockoff = pos & (grub_le_to_cpu32 (data->sblock.blksz) - 1);
  558. grub_uint32_t blockend = grub_le_to_cpu32 (data->sblock.blksz);
  559. grub_uint64_t skipfirst = 0;
  560. blknr = grub_jfs_blkno (data, &data->currinode, i);
  561. if (grub_errno)
  562. return -1;
  563. /* Last block. */
  564. if (i == blockcnt - 1)
  565. {
  566. blockend = (len + pos) & (grub_le_to_cpu32 (data->sblock.blksz) - 1);
  567. if (!blockend)
  568. blockend = grub_le_to_cpu32 (data->sblock.blksz);
  569. }
  570. /* First block. */
  571. if (i == (pos >> grub_le_to_cpu16 (data->sblock.log2_blksz)))
  572. {
  573. skipfirst = blockoff;
  574. blockend -= skipfirst;
  575. }
  576. data->disk->read_hook = read_hook;
  577. data->disk->read_hook_data = read_hook_data;
  578. grub_disk_read (data->disk,
  579. blknr << (grub_le_to_cpu16 (data->sblock.log2_blksz)
  580. - GRUB_DISK_SECTOR_BITS),
  581. skipfirst, blockend, buf);
  582. data->disk->read_hook = 0;
  583. if (grub_errno)
  584. return -1;
  585. buf += grub_le_to_cpu32 (data->sblock.blksz) - skipfirst;
  586. }
  587. return len;
  588. }
  589. /* Find the file with the pathname PATH on the filesystem described by
  590. DATA. */
  591. static grub_err_t
  592. grub_jfs_find_file (struct grub_jfs_data *data, const char *path,
  593. grub_uint32_t start_ino)
  594. {
  595. const char *name;
  596. const char *next = path;
  597. struct grub_jfs_diropen *diro = NULL;
  598. if (grub_jfs_read_inode (data, start_ino, &data->currinode))
  599. return grub_errno;
  600. while (1)
  601. {
  602. name = next;
  603. while (*name == '/')
  604. name++;
  605. if (name[0] == 0)
  606. return GRUB_ERR_NONE;
  607. for (next = name; *next && *next != '/'; next++);
  608. if (name[0] == '.' && name + 1 == next)
  609. continue;
  610. if (name[0] == '.' && name[1] == '.' && name + 2 == next)
  611. {
  612. grub_uint32_t ino = grub_le_to_cpu32 (data->currinode.dir.header.idotdot);
  613. if (grub_jfs_read_inode (data, ino, &data->currinode))
  614. return grub_errno;
  615. continue;
  616. }
  617. diro = grub_jfs_opendir (data, &data->currinode);
  618. if (!diro)
  619. return grub_errno;
  620. for (;;)
  621. {
  622. if (grub_jfs_getent (diro) == GRUB_ERR_OUT_OF_RANGE)
  623. {
  624. grub_jfs_closedir (diro);
  625. return grub_error (GRUB_ERR_FILE_NOT_FOUND, N_("file `%s' not found"), path);
  626. }
  627. /* Check if the current direntry matches the current part of the
  628. pathname. */
  629. if ((data->caseins ? grub_strncasecmp (name, diro->name, next - name) == 0
  630. : grub_strncmp (name, diro->name, next - name) == 0) && !diro->name[next - name])
  631. {
  632. grub_uint32_t ino = diro->ino;
  633. grub_uint32_t dirino = grub_le_to_cpu32 (data->currinode.inode);
  634. grub_jfs_closedir (diro);
  635. if (grub_jfs_read_inode (data, ino, &data->currinode))
  636. break;
  637. /* Check if this is a symlink. */
  638. if ((grub_le_to_cpu32 (data->currinode.mode)
  639. & GRUB_JFS_FILETYPE_MASK) == GRUB_JFS_FILETYPE_LNK)
  640. {
  641. grub_jfs_lookup_symlink (data, dirino);
  642. if (grub_errno)
  643. return grub_errno;
  644. }
  645. break;
  646. }
  647. }
  648. }
  649. }
  650. static grub_err_t
  651. grub_jfs_lookup_symlink (struct grub_jfs_data *data, grub_uint32_t ino)
  652. {
  653. grub_size_t size = grub_le_to_cpu64 (data->currinode.size);
  654. char *symlink;
  655. if (++data->linknest > GRUB_JFS_MAX_SYMLNK_CNT)
  656. return grub_error (GRUB_ERR_SYMLINK_LOOP, N_("too deep nesting of symlinks"));
  657. symlink = grub_malloc (size + 1);
  658. if (!symlink)
  659. return grub_errno;
  660. if (size <= sizeof (data->currinode.symlink.path))
  661. grub_memcpy (symlink, (char *) (data->currinode.symlink.path), size);
  662. else if (grub_jfs_read_file (data, 0, 0, 0, size, symlink) < 0)
  663. {
  664. grub_free (symlink);
  665. return grub_errno;
  666. }
  667. symlink[size] = '\0';
  668. /* The symlink is an absolute path, go back to the root inode. */
  669. if (symlink[0] == '/')
  670. ino = 2;
  671. grub_jfs_find_file (data, symlink, ino);
  672. grub_free (symlink);
  673. return grub_errno;
  674. }
  675. static grub_err_t
  676. grub_jfs_dir (grub_device_t device, const char *path,
  677. grub_fs_dir_hook_t hook, void *hook_data)
  678. {
  679. struct grub_jfs_data *data = 0;
  680. struct grub_jfs_diropen *diro = 0;
  681. grub_dl_ref (my_mod);
  682. data = grub_jfs_mount (device->disk);
  683. if (!data)
  684. goto fail;
  685. if (grub_jfs_find_file (data, path, GRUB_JFS_AGGR_INODE))
  686. goto fail;
  687. diro = grub_jfs_opendir (data, &data->currinode);
  688. if (!diro)
  689. goto fail;
  690. /* Iterate over the dirents in the directory that was found. */
  691. while (grub_jfs_getent (diro) != GRUB_ERR_OUT_OF_RANGE)
  692. {
  693. struct grub_jfs_inode inode;
  694. struct grub_dirhook_info info;
  695. grub_memset (&info, 0, sizeof (info));
  696. if (grub_jfs_read_inode (data, diro->ino, &inode))
  697. goto fail;
  698. info.dir = (grub_le_to_cpu32 (inode.mode)
  699. & GRUB_JFS_FILETYPE_MASK) == GRUB_JFS_FILETYPE_DIR;
  700. info.mtimeset = 1;
  701. info.mtime = grub_le_to_cpu32 (inode.mtime.sec);
  702. if (hook (diro->name, &info, hook_data))
  703. goto fail;
  704. }
  705. /* XXX: GRUB_ERR_OUT_OF_RANGE is used for the last dirent. */
  706. if (grub_errno == GRUB_ERR_OUT_OF_RANGE)
  707. grub_errno = 0;
  708. fail:
  709. grub_jfs_closedir (diro);
  710. grub_free (data);
  711. grub_dl_unref (my_mod);
  712. return grub_errno;
  713. }
  714. /* Open a file named NAME and initialize FILE. */
  715. static grub_err_t
  716. grub_jfs_open (struct grub_file *file, const char *name)
  717. {
  718. struct grub_jfs_data *data;
  719. grub_dl_ref (my_mod);
  720. data = grub_jfs_mount (file->device->disk);
  721. if (!data)
  722. goto fail;
  723. grub_jfs_find_file (data, name, GRUB_JFS_AGGR_INODE);
  724. if (grub_errno)
  725. goto fail;
  726. /* It is only possible for open regular files. */
  727. if (! ((grub_le_to_cpu32 (data->currinode.mode)
  728. & GRUB_JFS_FILETYPE_MASK) == GRUB_JFS_FILETYPE_REG))
  729. {
  730. grub_error (GRUB_ERR_BAD_FILE_TYPE, N_("not a regular file"));
  731. goto fail;
  732. }
  733. file->data = data;
  734. file->size = grub_le_to_cpu64 (data->currinode.size);
  735. return 0;
  736. fail:
  737. grub_dl_unref (my_mod);
  738. grub_free (data);
  739. return grub_errno;
  740. }
  741. static grub_ssize_t
  742. grub_jfs_read (grub_file_t file, char *buf, grub_size_t len)
  743. {
  744. struct grub_jfs_data *data =
  745. (struct grub_jfs_data *) file->data;
  746. return grub_jfs_read_file (data, file->read_hook, file->read_hook_data,
  747. file->offset, len, buf);
  748. }
  749. static grub_err_t
  750. grub_jfs_close (grub_file_t file)
  751. {
  752. grub_free (file->data);
  753. grub_dl_unref (my_mod);
  754. return GRUB_ERR_NONE;
  755. }
  756. static grub_err_t
  757. grub_jfs_uuid (grub_device_t device, char **uuid)
  758. {
  759. struct grub_jfs_data *data;
  760. grub_disk_t disk = device->disk;
  761. grub_dl_ref (my_mod);
  762. data = grub_jfs_mount (disk);
  763. if (data)
  764. {
  765. *uuid = grub_xasprintf ("%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-"
  766. "%02x%02x%02x%02x%02x%02x",
  767. data->sblock.uuid[0], data->sblock.uuid[1],
  768. data->sblock.uuid[2], data->sblock.uuid[3],
  769. data->sblock.uuid[4], data->sblock.uuid[5],
  770. data->sblock.uuid[6], data->sblock.uuid[7],
  771. data->sblock.uuid[8], data->sblock.uuid[9],
  772. data->sblock.uuid[10], data->sblock.uuid[11],
  773. data->sblock.uuid[12], data->sblock.uuid[13],
  774. data->sblock.uuid[14], data->sblock.uuid[15]);
  775. }
  776. else
  777. *uuid = NULL;
  778. grub_dl_unref (my_mod);
  779. grub_free (data);
  780. return grub_errno;
  781. }
  782. static grub_err_t
  783. grub_jfs_label (grub_device_t device, char **label)
  784. {
  785. struct grub_jfs_data *data;
  786. data = grub_jfs_mount (device->disk);
  787. if (data)
  788. {
  789. if (data->sblock.volname2[0] < ' ')
  790. {
  791. char *ptr;
  792. ptr = data->sblock.volname + sizeof (data->sblock.volname) - 1;
  793. while (ptr >= data->sblock.volname && *ptr == ' ')
  794. ptr--;
  795. *label = grub_strndup (data->sblock.volname,
  796. ptr - data->sblock.volname + 1);
  797. }
  798. else
  799. *label = grub_strndup (data->sblock.volname2,
  800. sizeof (data->sblock.volname2));
  801. }
  802. else
  803. *label = 0;
  804. grub_free (data);
  805. return grub_errno;
  806. }
  807. static struct grub_fs grub_jfs_fs =
  808. {
  809. .name = "jfs",
  810. .fs_dir = grub_jfs_dir,
  811. .fs_open = grub_jfs_open,
  812. .fs_read = grub_jfs_read,
  813. .fs_close = grub_jfs_close,
  814. .fs_label = grub_jfs_label,
  815. .fs_uuid = grub_jfs_uuid,
  816. #ifdef GRUB_UTIL
  817. .reserved_first_sector = 1,
  818. .blocklist_install = 1,
  819. #endif
  820. .next = 0
  821. };
  822. GRUB_MOD_INIT(jfs)
  823. {
  824. if (!grub_is_lockdown ())
  825. {
  826. grub_jfs_fs.mod = mod;
  827. grub_fs_register (&grub_jfs_fs);
  828. }
  829. my_mod = mod;
  830. }
  831. GRUB_MOD_FINI(jfs)
  832. {
  833. if (!grub_is_lockdown ())
  834. grub_fs_unregister (&grub_jfs_fs);
  835. }