jfs.c 24 KB

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