jfs.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956
  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. struct grub_jfs_data *data,
  228. grub_uint64_t blk)
  229. {
  230. int found = -1;
  231. int i;
  232. for (i = 0; i < grub_le_to_cpu16 (treehead->count) - 2; i++)
  233. {
  234. if (treehead->flags & GRUB_JFS_TREE_LEAF)
  235. {
  236. /* Read the leafnode. */
  237. if (grub_le_to_cpu32 (extents[i].offset2) <= blk
  238. && ((grub_le_to_cpu16 (extents[i].extent.length))
  239. + (extents[i].extent.length2 << 16)
  240. + grub_le_to_cpu32 (extents[i].offset2)) > blk)
  241. return (blk - grub_le_to_cpu32 (extents[i].offset2)
  242. + grub_le_to_cpu32 (extents[i].extent.blk2));
  243. }
  244. else
  245. if (blk >= grub_le_to_cpu32 (extents[i].offset2))
  246. found = i;
  247. }
  248. if (found != -1)
  249. {
  250. grub_int64_t ret = -1;
  251. struct
  252. {
  253. struct grub_jfs_treehead treehead;
  254. struct grub_jfs_tree_extent extents[254];
  255. } *tree;
  256. tree = grub_zalloc (sizeof (*tree));
  257. if (!tree)
  258. return -1;
  259. if (!grub_disk_read (data->disk,
  260. ((grub_disk_addr_t) grub_le_to_cpu32 (extents[found].extent.blk2))
  261. << (grub_le_to_cpu16 (data->sblock.log2_blksz)
  262. - GRUB_DISK_SECTOR_BITS), 0,
  263. sizeof (*tree), (char *) tree))
  264. ret = getblk (&tree->treehead, &tree->extents[0], data, blk);
  265. grub_free (tree);
  266. return ret;
  267. }
  268. return -1;
  269. }
  270. /* Get the block number for the block BLK in the node INODE in the
  271. mounted filesystem DATA. */
  272. static grub_int64_t
  273. grub_jfs_blkno (struct grub_jfs_data *data, struct grub_jfs_inode *inode,
  274. grub_uint64_t blk)
  275. {
  276. return getblk (&inode->file.tree, &inode->file.extents[0], data, blk);
  277. }
  278. static grub_err_t
  279. grub_jfs_read_inode (struct grub_jfs_data *data, grub_uint32_t ino,
  280. struct grub_jfs_inode *inode)
  281. {
  282. struct grub_jfs_extent iag_inodes[GRUB_JFS_IAG_INODES_COUNT];
  283. grub_uint32_t iagnum = ino / 4096;
  284. unsigned inoext = (ino % 4096) / 32;
  285. unsigned inonum = (ino % 4096) % 32;
  286. grub_uint64_t iagblk;
  287. grub_uint64_t inoblk;
  288. iagblk = grub_jfs_blkno (data, &data->fileset, iagnum + 1);
  289. if (grub_errno)
  290. return grub_errno;
  291. /* Read in the IAG. */
  292. if (grub_disk_read (data->disk,
  293. iagblk << (grub_le_to_cpu16 (data->sblock.log2_blksz)
  294. - GRUB_DISK_SECTOR_BITS),
  295. GRUB_JFS_IAG_INODES_OFFSET,
  296. sizeof (iag_inodes), &iag_inodes))
  297. return grub_errno;
  298. inoblk = grub_le_to_cpu32 (iag_inodes[inoext].blk2);
  299. inoblk <<= (grub_le_to_cpu16 (data->sblock.log2_blksz)
  300. - GRUB_DISK_SECTOR_BITS);
  301. inoblk += inonum;
  302. if (grub_disk_read (data->disk, inoblk, 0,
  303. sizeof (struct grub_jfs_inode), inode))
  304. return grub_errno;
  305. return 0;
  306. }
  307. static struct grub_jfs_data *
  308. grub_jfs_mount (grub_disk_t disk)
  309. {
  310. struct grub_jfs_data *data = 0;
  311. data = grub_malloc (sizeof (struct grub_jfs_data));
  312. if (!data)
  313. return 0;
  314. /* Read the superblock. */
  315. if (grub_disk_read (disk, GRUB_JFS_SBLOCK, 0,
  316. sizeof (struct grub_jfs_sblock), &data->sblock))
  317. goto fail;
  318. if (grub_strncmp ((char *) (data->sblock.magic), "JFS1", 4))
  319. {
  320. grub_error (GRUB_ERR_BAD_FS, "not a JFS filesystem");
  321. goto fail;
  322. }
  323. if (data->sblock.blksz == 0
  324. || grub_le_to_cpu32 (data->sblock.blksz)
  325. != (1U << grub_le_to_cpu16 (data->sblock.log2_blksz))
  326. || grub_le_to_cpu16 (data->sblock.log2_blksz) < GRUB_DISK_SECTOR_BITS)
  327. {
  328. grub_error (GRUB_ERR_BAD_FS, "not a JFS filesystem");
  329. goto fail;
  330. }
  331. data->disk = disk;
  332. data->pos = 0;
  333. data->linknest = 0;
  334. /* Read the inode of the first fileset. */
  335. if (grub_disk_read (data->disk, GRUB_JFS_FS1_INODE_BLK, 0,
  336. sizeof (struct grub_jfs_inode), &data->fileset))
  337. goto fail;
  338. if (data->sblock.flags & grub_cpu_to_le32_compile_time (0x00200000))
  339. data->namecomponentlen = 11;
  340. else
  341. data->namecomponentlen = 13;
  342. if (data->sblock.flags & grub_cpu_to_le32_compile_time (0x40000000))
  343. data->caseins = 1;
  344. else
  345. data->caseins = 0;
  346. return data;
  347. fail:
  348. grub_free (data);
  349. if (grub_errno == GRUB_ERR_OUT_OF_RANGE)
  350. grub_error (GRUB_ERR_BAD_FS, "not a JFS filesystem");
  351. return 0;
  352. }
  353. static struct grub_jfs_diropen *
  354. grub_jfs_opendir (struct grub_jfs_data *data, struct grub_jfs_inode *inode)
  355. {
  356. struct grub_jfs_internal_dirent *de;
  357. struct grub_jfs_diropen *diro;
  358. grub_disk_addr_t blk;
  359. de = (struct grub_jfs_internal_dirent *) inode->dir.dirents;
  360. if (!((grub_le_to_cpu32 (inode->mode)
  361. & GRUB_JFS_FILETYPE_MASK) == GRUB_JFS_FILETYPE_DIR))
  362. {
  363. grub_error (GRUB_ERR_BAD_FILE_TYPE, N_("not a directory"));
  364. return 0;
  365. }
  366. diro = grub_zalloc (sizeof (struct grub_jfs_diropen));
  367. if (!diro)
  368. return 0;
  369. diro->data = data;
  370. diro->inode = inode;
  371. /* Check if the entire tree is contained within the inode. */
  372. if (inode->file.tree.flags & GRUB_JFS_TREE_LEAF)
  373. {
  374. diro->leaf = inode->dir.dirents;
  375. diro->next_leaf = (struct grub_jfs_leaf_next_dirent *) de;
  376. diro->sorted = inode->dir.header.sorted;
  377. diro->count = inode->dir.header.count;
  378. return diro;
  379. }
  380. diro->dirpage = grub_malloc (grub_le_to_cpu32 (data->sblock.blksz));
  381. if (!diro->dirpage)
  382. {
  383. grub_free (diro);
  384. return 0;
  385. }
  386. blk = grub_le_to_cpu32 (de[inode->dir.header.sorted[0]].ex.blk2);
  387. blk <<= (grub_le_to_cpu16 (data->sblock.log2_blksz) - GRUB_DISK_SECTOR_BITS);
  388. /* Read in the nodes until we are on the leaf node level. */
  389. do
  390. {
  391. int index;
  392. if (grub_disk_read (data->disk, blk, 0,
  393. grub_le_to_cpu32 (data->sblock.blksz),
  394. diro->dirpage->sorted))
  395. {
  396. grub_free (diro->dirpage);
  397. grub_free (diro);
  398. return 0;
  399. }
  400. de = (struct grub_jfs_internal_dirent *) diro->dirpage->dirent;
  401. index = diro->dirpage->sorted[diro->dirpage->header.sindex * 32];
  402. blk = (grub_le_to_cpu32 (de[index].ex.blk2)
  403. << (grub_le_to_cpu16 (data->sblock.log2_blksz)
  404. - GRUB_DISK_SECTOR_BITS));
  405. } while (!(diro->dirpage->header.flags & GRUB_JFS_TREE_LEAF));
  406. diro->leaf = diro->dirpage->dirent;
  407. diro->next_leaf = diro->dirpage->next_dirent;
  408. diro->sorted = &diro->dirpage->sorted[diro->dirpage->header.sindex * 32];
  409. diro->count = diro->dirpage->header.count;
  410. return diro;
  411. }
  412. static void
  413. grub_jfs_closedir (struct grub_jfs_diropen *diro)
  414. {
  415. if (!diro)
  416. return;
  417. grub_free (diro->dirpage);
  418. grub_free (diro);
  419. }
  420. static void
  421. le_to_cpu16_copy (grub_uint16_t *out, grub_uint16_t *in, grub_size_t len)
  422. {
  423. while (len--)
  424. *out++ = grub_le_to_cpu16 (*in++);
  425. }
  426. /* Read in the next dirent from the directory described by DIRO. */
  427. static grub_err_t
  428. grub_jfs_getent (struct grub_jfs_diropen *diro)
  429. {
  430. int strpos = 0;
  431. struct grub_jfs_leaf_dirent *leaf;
  432. struct grub_jfs_leaf_next_dirent *next_leaf;
  433. int len;
  434. int nextent;
  435. grub_uint16_t filename[256];
  436. /* The last node, read in more. */
  437. if (diro->index == diro->count)
  438. {
  439. grub_disk_addr_t next;
  440. /* If the inode contains the entry tree or if this was the last
  441. node, there is nothing to read. */
  442. if ((diro->inode->file.tree.flags & GRUB_JFS_TREE_LEAF)
  443. || !grub_le_to_cpu64 (diro->dirpage->header.nextb))
  444. return GRUB_ERR_OUT_OF_RANGE;
  445. next = grub_le_to_cpu64 (diro->dirpage->header.nextb);
  446. next <<= (grub_le_to_cpu16 (diro->data->sblock.log2_blksz)
  447. - GRUB_DISK_SECTOR_BITS);
  448. if (grub_disk_read (diro->data->disk, next, 0,
  449. grub_le_to_cpu32 (diro->data->sblock.blksz),
  450. diro->dirpage->sorted))
  451. return grub_errno;
  452. diro->leaf = diro->dirpage->dirent;
  453. diro->next_leaf = diro->dirpage->next_dirent;
  454. diro->sorted = &diro->dirpage->sorted[diro->dirpage->header.sindex * 32];
  455. diro->count = diro->dirpage->header.count;
  456. diro->index = 0;
  457. }
  458. leaf = &diro->leaf[diro->sorted[diro->index]];
  459. next_leaf = &diro->next_leaf[diro->index];
  460. len = leaf->len;
  461. if (!len)
  462. {
  463. diro->index++;
  464. return grub_jfs_getent (diro);
  465. }
  466. le_to_cpu16_copy (filename + strpos, leaf->namepart, len < diro->data->namecomponentlen ? len
  467. : diro->data->namecomponentlen);
  468. strpos += len < diro->data->namecomponentlen ? len
  469. : diro->data->namecomponentlen;
  470. diro->ino = grub_le_to_cpu32 (leaf->inode);
  471. len -= diro->data->namecomponentlen;
  472. /* Move down to the leaf level. */
  473. nextent = leaf->next;
  474. if (leaf->next != 255)
  475. do
  476. {
  477. next_leaf = &diro->next_leaf[nextent];
  478. le_to_cpu16_copy (filename + strpos, next_leaf->namepart, len < 15 ? len : 15);
  479. strpos += len < 15 ? len : 15;
  480. len -= 15;
  481. nextent = next_leaf->next;
  482. } while (next_leaf->next != 255 && len > 0);
  483. diro->index++;
  484. /* Convert the temporary UTF16 filename to UTF8. */
  485. *grub_utf16_to_utf8 ((grub_uint8_t *) (diro->name), filename, strpos) = '\0';
  486. return 0;
  487. }
  488. /* Read LEN bytes from the file described by DATA starting with byte
  489. POS. Return the amount of read bytes in READ. */
  490. static grub_ssize_t
  491. grub_jfs_read_file (struct grub_jfs_data *data,
  492. grub_disk_read_hook_t read_hook, void *read_hook_data,
  493. grub_off_t pos, grub_size_t len, char *buf)
  494. {
  495. grub_off_t i;
  496. grub_off_t blockcnt;
  497. blockcnt = (len + pos + grub_le_to_cpu32 (data->sblock.blksz) - 1)
  498. >> grub_le_to_cpu16 (data->sblock.log2_blksz);
  499. for (i = pos >> grub_le_to_cpu16 (data->sblock.log2_blksz); i < blockcnt; i++)
  500. {
  501. grub_disk_addr_t blknr;
  502. grub_uint32_t blockoff = pos & (grub_le_to_cpu32 (data->sblock.blksz) - 1);
  503. grub_uint32_t blockend = grub_le_to_cpu32 (data->sblock.blksz);
  504. grub_uint64_t skipfirst = 0;
  505. blknr = grub_jfs_blkno (data, &data->currinode, i);
  506. if (grub_errno)
  507. return -1;
  508. /* Last block. */
  509. if (i == blockcnt - 1)
  510. {
  511. blockend = (len + pos) & (grub_le_to_cpu32 (data->sblock.blksz) - 1);
  512. if (!blockend)
  513. blockend = grub_le_to_cpu32 (data->sblock.blksz);
  514. }
  515. /* First block. */
  516. if (i == (pos >> grub_le_to_cpu16 (data->sblock.log2_blksz)))
  517. {
  518. skipfirst = blockoff;
  519. blockend -= skipfirst;
  520. }
  521. data->disk->read_hook = read_hook;
  522. data->disk->read_hook_data = read_hook_data;
  523. grub_disk_read (data->disk,
  524. blknr << (grub_le_to_cpu16 (data->sblock.log2_blksz)
  525. - GRUB_DISK_SECTOR_BITS),
  526. skipfirst, blockend, buf);
  527. data->disk->read_hook = 0;
  528. if (grub_errno)
  529. return -1;
  530. buf += grub_le_to_cpu32 (data->sblock.blksz) - skipfirst;
  531. }
  532. return len;
  533. }
  534. /* Find the file with the pathname PATH on the filesystem described by
  535. DATA. */
  536. static grub_err_t
  537. grub_jfs_find_file (struct grub_jfs_data *data, const char *path,
  538. grub_uint32_t start_ino)
  539. {
  540. const char *name;
  541. const char *next = path;
  542. struct grub_jfs_diropen *diro = NULL;
  543. if (grub_jfs_read_inode (data, start_ino, &data->currinode))
  544. return grub_errno;
  545. while (1)
  546. {
  547. name = next;
  548. while (*name == '/')
  549. name++;
  550. if (name[0] == 0)
  551. return GRUB_ERR_NONE;
  552. for (next = name; *next && *next != '/'; next++);
  553. if (name[0] == '.' && name + 1 == next)
  554. continue;
  555. if (name[0] == '.' && name[1] == '.' && name + 2 == next)
  556. {
  557. grub_uint32_t ino = grub_le_to_cpu32 (data->currinode.dir.header.idotdot);
  558. if (grub_jfs_read_inode (data, ino, &data->currinode))
  559. return grub_errno;
  560. continue;
  561. }
  562. diro = grub_jfs_opendir (data, &data->currinode);
  563. if (!diro)
  564. return grub_errno;
  565. for (;;)
  566. {
  567. if (grub_jfs_getent (diro) == GRUB_ERR_OUT_OF_RANGE)
  568. {
  569. grub_jfs_closedir (diro);
  570. return grub_error (GRUB_ERR_FILE_NOT_FOUND, N_("file `%s' not found"), path);
  571. }
  572. /* Check if the current direntry matches the current part of the
  573. pathname. */
  574. if ((data->caseins ? grub_strncasecmp (name, diro->name, next - name) == 0
  575. : grub_strncmp (name, diro->name, next - name) == 0) && !diro->name[next - name])
  576. {
  577. grub_uint32_t ino = diro->ino;
  578. grub_uint32_t dirino = grub_le_to_cpu32 (data->currinode.inode);
  579. grub_jfs_closedir (diro);
  580. diro = 0;
  581. if (grub_jfs_read_inode (data, ino, &data->currinode))
  582. break;
  583. /* Check if this is a symlink. */
  584. if ((grub_le_to_cpu32 (data->currinode.mode)
  585. & GRUB_JFS_FILETYPE_MASK) == GRUB_JFS_FILETYPE_LNK)
  586. {
  587. grub_jfs_lookup_symlink (data, dirino);
  588. if (grub_errno)
  589. return grub_errno;
  590. }
  591. break;
  592. }
  593. }
  594. }
  595. }
  596. static grub_err_t
  597. grub_jfs_lookup_symlink (struct grub_jfs_data *data, grub_uint32_t ino)
  598. {
  599. grub_size_t size = grub_le_to_cpu64 (data->currinode.size);
  600. char *symlink;
  601. if (++data->linknest > GRUB_JFS_MAX_SYMLNK_CNT)
  602. return grub_error (GRUB_ERR_SYMLINK_LOOP, N_("too deep nesting of symlinks"));
  603. symlink = grub_malloc (size + 1);
  604. if (!symlink)
  605. return grub_errno;
  606. if (size <= sizeof (data->currinode.symlink.path))
  607. grub_memcpy (symlink, (char *) (data->currinode.symlink.path), size);
  608. else if (grub_jfs_read_file (data, 0, 0, 0, size, symlink) < 0)
  609. {
  610. grub_free (symlink);
  611. return grub_errno;
  612. }
  613. symlink[size] = '\0';
  614. /* The symlink is an absolute path, go back to the root inode. */
  615. if (symlink[0] == '/')
  616. ino = 2;
  617. grub_jfs_find_file (data, symlink, ino);
  618. grub_free (symlink);
  619. return grub_errno;
  620. }
  621. static grub_err_t
  622. grub_jfs_dir (grub_device_t device, const char *path,
  623. grub_fs_dir_hook_t hook, void *hook_data)
  624. {
  625. struct grub_jfs_data *data = 0;
  626. struct grub_jfs_diropen *diro = 0;
  627. grub_dl_ref (my_mod);
  628. data = grub_jfs_mount (device->disk);
  629. if (!data)
  630. goto fail;
  631. if (grub_jfs_find_file (data, path, GRUB_JFS_AGGR_INODE))
  632. goto fail;
  633. diro = grub_jfs_opendir (data, &data->currinode);
  634. if (!diro)
  635. goto fail;
  636. /* Iterate over the dirents in the directory that was found. */
  637. while (grub_jfs_getent (diro) != GRUB_ERR_OUT_OF_RANGE)
  638. {
  639. struct grub_jfs_inode inode;
  640. struct grub_dirhook_info info;
  641. grub_memset (&info, 0, sizeof (info));
  642. if (grub_jfs_read_inode (data, diro->ino, &inode))
  643. goto fail;
  644. info.dir = (grub_le_to_cpu32 (inode.mode)
  645. & GRUB_JFS_FILETYPE_MASK) == GRUB_JFS_FILETYPE_DIR;
  646. info.mtimeset = 1;
  647. info.mtime = grub_le_to_cpu32 (inode.mtime.sec);
  648. if (hook (diro->name, &info, hook_data))
  649. goto fail;
  650. }
  651. /* XXX: GRUB_ERR_OUT_OF_RANGE is used for the last dirent. */
  652. if (grub_errno == GRUB_ERR_OUT_OF_RANGE)
  653. grub_errno = 0;
  654. fail:
  655. grub_jfs_closedir (diro);
  656. grub_free (data);
  657. grub_dl_unref (my_mod);
  658. return grub_errno;
  659. }
  660. /* Open a file named NAME and initialize FILE. */
  661. static grub_err_t
  662. grub_jfs_open (struct grub_file *file, const char *name)
  663. {
  664. struct grub_jfs_data *data;
  665. grub_dl_ref (my_mod);
  666. data = grub_jfs_mount (file->device->disk);
  667. if (!data)
  668. goto fail;
  669. grub_jfs_find_file (data, name, GRUB_JFS_AGGR_INODE);
  670. if (grub_errno)
  671. goto fail;
  672. /* It is only possible for open regular files. */
  673. if (! ((grub_le_to_cpu32 (data->currinode.mode)
  674. & GRUB_JFS_FILETYPE_MASK) == GRUB_JFS_FILETYPE_REG))
  675. {
  676. grub_error (GRUB_ERR_BAD_FILE_TYPE, N_("not a regular file"));
  677. goto fail;
  678. }
  679. file->data = data;
  680. file->size = grub_le_to_cpu64 (data->currinode.size);
  681. return 0;
  682. fail:
  683. grub_dl_unref (my_mod);
  684. grub_free (data);
  685. return grub_errno;
  686. }
  687. static grub_ssize_t
  688. grub_jfs_read (grub_file_t file, char *buf, grub_size_t len)
  689. {
  690. struct grub_jfs_data *data =
  691. (struct grub_jfs_data *) file->data;
  692. return grub_jfs_read_file (data, file->read_hook, file->read_hook_data,
  693. file->offset, len, buf);
  694. }
  695. static grub_err_t
  696. grub_jfs_close (grub_file_t file)
  697. {
  698. grub_free (file->data);
  699. grub_dl_unref (my_mod);
  700. return GRUB_ERR_NONE;
  701. }
  702. static grub_err_t
  703. grub_jfs_uuid (grub_device_t device, char **uuid)
  704. {
  705. struct grub_jfs_data *data;
  706. grub_disk_t disk = device->disk;
  707. grub_dl_ref (my_mod);
  708. data = grub_jfs_mount (disk);
  709. if (data)
  710. {
  711. *uuid = grub_xasprintf ("%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-"
  712. "%02x%02x%02x%02x%02x%02x",
  713. data->sblock.uuid[0], data->sblock.uuid[1],
  714. data->sblock.uuid[2], data->sblock.uuid[3],
  715. data->sblock.uuid[4], data->sblock.uuid[5],
  716. data->sblock.uuid[6], data->sblock.uuid[7],
  717. data->sblock.uuid[8], data->sblock.uuid[9],
  718. data->sblock.uuid[10], data->sblock.uuid[11],
  719. data->sblock.uuid[12], data->sblock.uuid[13],
  720. data->sblock.uuid[14], data->sblock.uuid[15]);
  721. }
  722. else
  723. *uuid = NULL;
  724. grub_dl_unref (my_mod);
  725. grub_free (data);
  726. return grub_errno;
  727. }
  728. static grub_err_t
  729. grub_jfs_label (grub_device_t device, char **label)
  730. {
  731. struct grub_jfs_data *data;
  732. data = grub_jfs_mount (device->disk);
  733. if (data)
  734. {
  735. if (data->sblock.volname2[0] < ' ')
  736. {
  737. char *ptr;
  738. ptr = data->sblock.volname + sizeof (data->sblock.volname) - 1;
  739. while (ptr >= data->sblock.volname && *ptr == ' ')
  740. ptr--;
  741. *label = grub_strndup (data->sblock.volname,
  742. ptr - data->sblock.volname + 1);
  743. }
  744. else
  745. *label = grub_strndup (data->sblock.volname2,
  746. sizeof (data->sblock.volname2));
  747. }
  748. else
  749. *label = 0;
  750. grub_free (data);
  751. return grub_errno;
  752. }
  753. static struct grub_fs grub_jfs_fs =
  754. {
  755. .name = "jfs",
  756. .dir = grub_jfs_dir,
  757. .open = grub_jfs_open,
  758. .read = grub_jfs_read,
  759. .close = grub_jfs_close,
  760. .label = grub_jfs_label,
  761. .uuid = grub_jfs_uuid,
  762. #ifdef GRUB_UTIL
  763. .reserved_first_sector = 1,
  764. .blocklist_install = 1,
  765. #endif
  766. .next = 0
  767. };
  768. GRUB_MOD_INIT(jfs)
  769. {
  770. grub_fs_register (&grub_jfs_fs);
  771. my_mod = mod;
  772. }
  773. GRUB_MOD_FINI(jfs)
  774. {
  775. grub_fs_unregister (&grub_jfs_fs);
  776. }