minix.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  1. /* minix.c - The minix filesystem, version 1 and 2. */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2004,2005,2006,2007,2008 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/i18n.h>
  27. #include <grub/lockdown.h>
  28. GRUB_MOD_LICENSE ("GPLv3+");
  29. #ifdef MODE_MINIX3
  30. #define GRUB_MINIX_MAGIC 0x4D5A
  31. #elif defined(MODE_MINIX2)
  32. #define GRUB_MINIX_MAGIC 0x2468
  33. #define GRUB_MINIX_MAGIC_30 0x2478
  34. #else
  35. #define GRUB_MINIX_MAGIC 0x137F
  36. #define GRUB_MINIX_MAGIC_30 0x138F
  37. #endif
  38. #define EXT2_MAGIC 0xEF53
  39. #define GRUB_MINIX_INODE_DIR_BLOCKS 7
  40. #define GRUB_MINIX_LOG2_BSIZE 1
  41. #define GRUB_MINIX_ROOT_INODE 1
  42. #define GRUB_MINIX_MAX_SYMLNK_CNT 8
  43. #define GRUB_MINIX_SBLOCK 2
  44. #define GRUB_MINIX_IFDIR 0040000U
  45. #define GRUB_MINIX_IFLNK 0120000U
  46. #ifdef MODE_BIGENDIAN
  47. #define grub_cpu_to_minix16_compile_time grub_cpu_to_be16_compile_time
  48. #define grub_minix_to_cpu16 grub_be_to_cpu16
  49. #define grub_minix_to_cpu32 grub_be_to_cpu32
  50. #else
  51. #define grub_cpu_to_minix16_compile_time grub_cpu_to_le16_compile_time
  52. #define grub_minix_to_cpu16 grub_le_to_cpu16
  53. #define grub_minix_to_cpu32 grub_le_to_cpu32
  54. #endif
  55. #if defined(MODE_MINIX2) || defined(MODE_MINIX3)
  56. typedef grub_uint32_t grub_minix_uintn_t;
  57. #define grub_minix_to_cpu_n grub_minix_to_cpu32
  58. #else
  59. typedef grub_uint16_t grub_minix_uintn_t;
  60. #define grub_minix_to_cpu_n grub_minix_to_cpu16
  61. #endif
  62. #ifdef MODE_MINIX3
  63. typedef grub_uint32_t grub_minix_ino_t;
  64. #define grub_minix_to_cpu_ino grub_minix_to_cpu32
  65. #else
  66. typedef grub_uint16_t grub_minix_ino_t;
  67. #define grub_minix_to_cpu_ino grub_minix_to_cpu16
  68. #endif
  69. #define GRUB_MINIX_INODE_SIZE(data) (grub_minix_to_cpu32 (data->inode.size))
  70. #define GRUB_MINIX_INODE_MODE(data) (grub_minix_to_cpu16 (data->inode.mode))
  71. #define GRUB_MINIX_INODE_DIR_ZONES(data,blk) (grub_minix_to_cpu_n \
  72. (data->inode.dir_zones[blk]))
  73. #define GRUB_MINIX_INODE_INDIR_ZONE(data) (grub_minix_to_cpu_n \
  74. (data->inode.indir_zone))
  75. #define GRUB_MINIX_INODE_DINDIR_ZONE(data) (grub_minix_to_cpu_n \
  76. (data->inode.double_indir_zone))
  77. #ifdef MODE_MINIX3
  78. struct grub_minix_sblock
  79. {
  80. grub_uint32_t inode_cnt;
  81. grub_uint16_t zone_cnt;
  82. grub_uint16_t inode_bmap_size;
  83. grub_uint16_t zone_bmap_size;
  84. grub_uint16_t first_data_zone;
  85. grub_uint16_t log2_zone_size;
  86. grub_uint16_t pad;
  87. grub_uint32_t max_file_size;
  88. grub_uint32_t zones;
  89. grub_uint16_t magic;
  90. grub_uint16_t pad2;
  91. grub_uint16_t block_size;
  92. grub_uint8_t disk_version;
  93. };
  94. #else
  95. struct grub_minix_sblock
  96. {
  97. grub_uint16_t inode_cnt;
  98. grub_uint16_t zone_cnt;
  99. grub_uint16_t inode_bmap_size;
  100. grub_uint16_t zone_bmap_size;
  101. grub_uint16_t first_data_zone;
  102. grub_uint16_t log2_zone_size;
  103. grub_uint32_t max_file_size;
  104. grub_uint16_t magic;
  105. };
  106. #endif
  107. #if defined(MODE_MINIX3) || defined(MODE_MINIX2)
  108. struct grub_minix_inode
  109. {
  110. grub_uint16_t mode;
  111. grub_uint16_t nlinks;
  112. grub_uint16_t uid;
  113. grub_uint16_t gid;
  114. grub_uint32_t size;
  115. grub_uint32_t atime;
  116. grub_uint32_t mtime;
  117. grub_uint32_t ctime;
  118. grub_uint32_t dir_zones[7];
  119. grub_uint32_t indir_zone;
  120. grub_uint32_t double_indir_zone;
  121. grub_uint32_t triple_indir_zone;
  122. };
  123. #else
  124. struct grub_minix_inode
  125. {
  126. grub_uint16_t mode;
  127. grub_uint16_t uid;
  128. grub_uint32_t size;
  129. grub_uint32_t mtime;
  130. grub_uint8_t gid;
  131. grub_uint8_t nlinks;
  132. grub_uint16_t dir_zones[7];
  133. grub_uint16_t indir_zone;
  134. grub_uint16_t double_indir_zone;
  135. };
  136. #endif
  137. #if defined(MODE_MINIX3)
  138. #define MAX_MINIX_FILENAME_SIZE 60
  139. #else
  140. #define MAX_MINIX_FILENAME_SIZE 30
  141. #endif
  142. /* Information about a "mounted" minix filesystem. */
  143. struct grub_minix_data
  144. {
  145. struct grub_minix_sblock sblock;
  146. struct grub_minix_inode inode;
  147. grub_uint32_t block_per_zone;
  148. grub_minix_ino_t ino;
  149. int linknest;
  150. grub_disk_t disk;
  151. int filename_size;
  152. grub_size_t block_size;
  153. };
  154. static grub_dl_t my_mod;
  155. static grub_err_t grub_minix_find_file (struct grub_minix_data *data,
  156. const char *path);
  157. #ifdef MODE_MINIX3
  158. static inline grub_disk_addr_t
  159. grub_minix_zone2sect (struct grub_minix_data *data, grub_minix_uintn_t zone)
  160. {
  161. return ((grub_disk_addr_t) zone) * data->block_size;
  162. }
  163. #else
  164. static inline grub_disk_addr_t
  165. grub_minix_zone2sect (struct grub_minix_data *data, grub_minix_uintn_t zone)
  166. {
  167. int log2_zonesz = (GRUB_MINIX_LOG2_BSIZE
  168. + grub_minix_to_cpu16 (data->sblock.log2_zone_size));
  169. return (((grub_disk_addr_t) zone) << log2_zonesz);
  170. }
  171. #endif
  172. /* Read the block pointer in ZONE, on the offset NUM. */
  173. static grub_minix_uintn_t
  174. grub_get_indir (struct grub_minix_data *data,
  175. grub_minix_uintn_t zone,
  176. grub_minix_uintn_t num)
  177. {
  178. grub_minix_uintn_t indirn;
  179. grub_disk_read (data->disk,
  180. grub_minix_zone2sect(data, zone),
  181. sizeof (grub_minix_uintn_t) * num,
  182. sizeof (grub_minix_uintn_t), (char *) &indirn);
  183. return grub_minix_to_cpu_n (indirn);
  184. }
  185. static grub_minix_uintn_t
  186. grub_minix_get_file_block (struct grub_minix_data *data, unsigned int blk)
  187. {
  188. grub_minix_uintn_t indir;
  189. /* Direct block. */
  190. if (blk < GRUB_MINIX_INODE_DIR_BLOCKS)
  191. return GRUB_MINIX_INODE_DIR_ZONES (data, blk);
  192. /* Indirect block. */
  193. blk -= GRUB_MINIX_INODE_DIR_BLOCKS;
  194. if (blk < data->block_per_zone)
  195. {
  196. indir = grub_get_indir (data, GRUB_MINIX_INODE_INDIR_ZONE (data), blk);
  197. return indir;
  198. }
  199. /* Double indirect block. */
  200. blk -= data->block_per_zone;
  201. if (blk < (grub_uint64_t) data->block_per_zone * (grub_uint64_t) data->block_per_zone)
  202. {
  203. indir = grub_get_indir (data, GRUB_MINIX_INODE_DINDIR_ZONE (data),
  204. blk / data->block_per_zone);
  205. indir = grub_get_indir (data, indir, blk % data->block_per_zone);
  206. return indir;
  207. }
  208. #if defined (MODE_MINIX3) || defined (MODE_MINIX2)
  209. blk -= data->block_per_zone * data->block_per_zone;
  210. if (blk < ((grub_uint64_t) data->block_per_zone * (grub_uint64_t) data->block_per_zone
  211. * (grub_uint64_t) data->block_per_zone))
  212. {
  213. indir = grub_get_indir (data, grub_minix_to_cpu_n (data->inode.triple_indir_zone),
  214. (blk / data->block_per_zone) / data->block_per_zone);
  215. indir = grub_get_indir (data, indir, (blk / data->block_per_zone) % data->block_per_zone);
  216. indir = grub_get_indir (data, indir, blk % data->block_per_zone);
  217. return indir;
  218. }
  219. #endif
  220. /* This should never happen. */
  221. grub_error (GRUB_ERR_OUT_OF_RANGE, "file bigger than maximum size");
  222. return 0;
  223. }
  224. /* Read LEN bytes from the file described by DATA starting with byte
  225. POS. Return the amount of read bytes in READ. */
  226. static grub_ssize_t
  227. grub_minix_read_file (struct grub_minix_data *data,
  228. grub_disk_read_hook_t read_hook, void *read_hook_data,
  229. grub_off_t pos, grub_size_t len, char *buf)
  230. {
  231. grub_uint32_t i;
  232. grub_uint32_t blockcnt;
  233. grub_uint32_t posblock;
  234. grub_uint32_t blockoff;
  235. if (pos > GRUB_MINIX_INODE_SIZE (data))
  236. {
  237. grub_error (GRUB_ERR_OUT_OF_RANGE,
  238. N_("attempt to read past the end of file"));
  239. return -1;
  240. }
  241. /* Adjust len so it we can't read past the end of the file. */
  242. if (len + pos > GRUB_MINIX_INODE_SIZE (data))
  243. len = GRUB_MINIX_INODE_SIZE (data) - pos;
  244. if (len == 0)
  245. return 0;
  246. /* Files are at most 2G/4G - 1 bytes on minixfs. Avoid 64-bit division. */
  247. blockcnt = ((grub_uint32_t) ((len + pos - 1)
  248. >> GRUB_DISK_SECTOR_BITS)) / data->block_size + 1;
  249. posblock = (((grub_uint32_t) pos)
  250. / (data->block_size << GRUB_DISK_SECTOR_BITS));
  251. blockoff = (((grub_uint32_t) pos)
  252. % (data->block_size << GRUB_DISK_SECTOR_BITS));
  253. for (i = posblock; i < blockcnt; i++)
  254. {
  255. grub_minix_uintn_t blknr;
  256. grub_uint64_t blockend = data->block_size << GRUB_DISK_SECTOR_BITS;
  257. grub_off_t skipfirst = 0;
  258. blknr = grub_minix_get_file_block (data, i);
  259. if (grub_errno)
  260. return -1;
  261. /* Last block. */
  262. if (i == blockcnt - 1)
  263. {
  264. /* len + pos < 4G (checked above), so it doesn't overflow. */
  265. blockend = (((grub_uint32_t) (len + pos))
  266. % (data->block_size << GRUB_DISK_SECTOR_BITS));
  267. if (!blockend)
  268. blockend = data->block_size << GRUB_DISK_SECTOR_BITS;
  269. }
  270. /* First block. */
  271. if (i == posblock)
  272. {
  273. skipfirst = blockoff;
  274. blockend -= skipfirst;
  275. }
  276. data->disk->read_hook = read_hook;
  277. data->disk->read_hook_data = read_hook_data;
  278. grub_disk_read (data->disk,
  279. grub_minix_zone2sect(data, blknr),
  280. skipfirst, blockend, buf);
  281. data->disk->read_hook = 0;
  282. if (grub_errno)
  283. return -1;
  284. buf += (data->block_size << GRUB_DISK_SECTOR_BITS) - skipfirst;
  285. }
  286. return len;
  287. }
  288. /* Read inode INO from the mounted filesystem described by DATA. This
  289. inode is used by default now. */
  290. static grub_err_t
  291. grub_minix_read_inode (struct grub_minix_data *data, grub_minix_ino_t ino)
  292. {
  293. struct grub_minix_sblock *sblock = &data->sblock;
  294. /* Block in which the inode is stored. */
  295. grub_disk_addr_t block;
  296. data->ino = ino;
  297. /* The first inode in minix is inode 1. */
  298. ino--;
  299. block = grub_minix_zone2sect (data,
  300. 2 + grub_minix_to_cpu16 (sblock->inode_bmap_size)
  301. + grub_minix_to_cpu16 (sblock->zone_bmap_size));
  302. block += ino / (GRUB_DISK_SECTOR_SIZE / sizeof (struct grub_minix_inode));
  303. int offs = (ino % (GRUB_DISK_SECTOR_SIZE
  304. / sizeof (struct grub_minix_inode))
  305. * sizeof (struct grub_minix_inode));
  306. grub_disk_read (data->disk, block, offs,
  307. sizeof (struct grub_minix_inode), &data->inode);
  308. return GRUB_ERR_NONE;
  309. }
  310. /* Lookup the symlink the current inode points to. INO is the inode
  311. number of the directory the symlink is relative to. */
  312. static grub_err_t
  313. grub_minix_lookup_symlink (struct grub_minix_data *data, grub_minix_ino_t ino)
  314. {
  315. char *symlink;
  316. grub_size_t sz = GRUB_MINIX_INODE_SIZE (data);
  317. if (++data->linknest > GRUB_MINIX_MAX_SYMLNK_CNT)
  318. return grub_error (GRUB_ERR_SYMLINK_LOOP, N_("too deep nesting of symlinks"));
  319. symlink = grub_malloc (sz + 1);
  320. if (!symlink)
  321. return grub_errno;
  322. if (grub_minix_read_file (data, 0, 0, 0, sz, symlink) < 0)
  323. goto fail;
  324. symlink[sz] = '\0';
  325. /* The symlink is an absolute path, go back to the root inode. */
  326. if (symlink[0] == '/')
  327. ino = GRUB_MINIX_ROOT_INODE;
  328. /* Now load in the old inode. */
  329. if (grub_minix_read_inode (data, ino))
  330. goto fail;
  331. grub_minix_find_file (data, symlink);
  332. fail:
  333. grub_free(symlink);
  334. return grub_errno;
  335. }
  336. /* Find the file with the pathname PATH on the filesystem described by
  337. DATA. */
  338. static grub_err_t
  339. grub_minix_find_file (struct grub_minix_data *data, const char *path)
  340. {
  341. const char *name;
  342. const char *next = path;
  343. unsigned int pos = 0;
  344. grub_minix_ino_t dirino;
  345. while (1)
  346. {
  347. name = next;
  348. /* Skip the first slash. */
  349. while (*name == '/')
  350. name++;
  351. if (!*name)
  352. return GRUB_ERR_NONE;
  353. if ((GRUB_MINIX_INODE_MODE (data)
  354. & GRUB_MINIX_IFDIR) != GRUB_MINIX_IFDIR)
  355. return grub_error (GRUB_ERR_BAD_FILE_TYPE, N_("not a directory"));
  356. /* Extract the actual part from the pathname. */
  357. for (next = name; *next && *next != '/'; next++);
  358. for (pos = 0; ; )
  359. {
  360. grub_minix_ino_t ino;
  361. char filename[MAX_MINIX_FILENAME_SIZE + 1];
  362. if (pos >= GRUB_MINIX_INODE_SIZE (data))
  363. {
  364. grub_error (GRUB_ERR_FILE_NOT_FOUND, N_("file `%s' not found"), path);
  365. return grub_errno;
  366. }
  367. if (grub_minix_read_file (data, 0, 0, pos, sizeof (ino),
  368. (char *) &ino) < 0)
  369. return grub_errno;
  370. if (grub_minix_read_file (data, 0, 0, pos + sizeof (ino),
  371. data->filename_size, (char *) filename)< 0)
  372. return grub_errno;
  373. pos += sizeof (ino) + data->filename_size;
  374. filename[data->filename_size] = '\0';
  375. /* Check if the current direntry matches the current part of the
  376. pathname. */
  377. if (grub_strncmp (name, filename, next - name) == 0
  378. && filename[next - name] == '\0')
  379. {
  380. dirino = data->ino;
  381. grub_minix_read_inode (data, grub_minix_to_cpu_ino (ino));
  382. /* Follow the symlink. */
  383. if ((GRUB_MINIX_INODE_MODE (data)
  384. & GRUB_MINIX_IFLNK) == GRUB_MINIX_IFLNK)
  385. {
  386. grub_minix_lookup_symlink (data, dirino);
  387. if (grub_errno)
  388. return grub_errno;
  389. }
  390. break;
  391. }
  392. }
  393. }
  394. }
  395. /* Mount the filesystem on the disk DISK. */
  396. static struct grub_minix_data *
  397. grub_minix_mount (grub_disk_t disk)
  398. {
  399. struct grub_minix_data *data = NULL;
  400. grub_uint16_t ext2_marker;
  401. grub_disk_read (disk, 2, 56, sizeof (ext2_marker), &ext2_marker);
  402. if (grub_errno != GRUB_ERR_NONE)
  403. goto fail;
  404. /*
  405. * The ext2 filesystems can sometimes be mistakenly identified as MINIX, e.g.
  406. * due to the number of free ext2 inodes being written to the same location
  407. * where the MINIX superblock magic is found. Avoid such situations by
  408. * skipping any filesystems that have the ext2 superblock magic.
  409. */
  410. if (ext2_marker == grub_cpu_to_le16_compile_time (EXT2_MAGIC))
  411. goto fail;
  412. data = grub_malloc (sizeof (struct grub_minix_data));
  413. if (!data)
  414. return 0;
  415. /* Read the superblock. */
  416. grub_disk_read (disk, GRUB_MINIX_SBLOCK, 0,
  417. sizeof (struct grub_minix_sblock),&data->sblock);
  418. if (grub_errno)
  419. goto fail;
  420. if (data->sblock.magic == grub_cpu_to_minix16_compile_time (GRUB_MINIX_MAGIC))
  421. {
  422. #if !defined(MODE_MINIX3)
  423. data->filename_size = 14;
  424. #else
  425. data->filename_size = 60;
  426. #endif
  427. }
  428. #if !defined(MODE_MINIX3)
  429. else if (data->sblock.magic
  430. == grub_cpu_to_minix16_compile_time (GRUB_MINIX_MAGIC_30))
  431. data->filename_size = 30;
  432. #endif
  433. else
  434. goto fail;
  435. /* 20 means 1G zones. We could support up to 31 but already 1G isn't
  436. supported by anything else. */
  437. if (grub_minix_to_cpu16 (data->sblock.log2_zone_size) >= 20)
  438. goto fail;
  439. data->disk = disk;
  440. data->linknest = 0;
  441. #ifdef MODE_MINIX3
  442. /* These tests are endian-independent. No need to byteswap. */
  443. if (data->sblock.block_size == 0xffff)
  444. data->block_size = 2;
  445. else
  446. {
  447. if ((data->sblock.block_size == grub_cpu_to_minix16_compile_time (0x200))
  448. || (data->sblock.block_size == 0)
  449. || (data->sblock.block_size & grub_cpu_to_minix16_compile_time (0x1ff)))
  450. goto fail;
  451. data->block_size = grub_minix_to_cpu16 (data->sblock.block_size)
  452. >> GRUB_DISK_SECTOR_BITS;
  453. }
  454. #else
  455. data->block_size = 2;
  456. #endif
  457. data->block_per_zone = (((grub_uint64_t) data->block_size << \
  458. (GRUB_DISK_SECTOR_BITS + grub_minix_to_cpu16 (data->sblock.log2_zone_size)))
  459. / sizeof (grub_minix_uintn_t));
  460. if (!data->block_per_zone)
  461. goto fail;
  462. return data;
  463. fail:
  464. grub_free (data);
  465. #if defined(MODE_MINIX3)
  466. grub_error (GRUB_ERR_BAD_FS, "not a minix3 filesystem");
  467. #elif defined(MODE_MINIX2)
  468. grub_error (GRUB_ERR_BAD_FS, "not a minix2 filesystem");
  469. #else
  470. grub_error (GRUB_ERR_BAD_FS, "not a minix filesystem");
  471. #endif
  472. return 0;
  473. }
  474. static grub_err_t
  475. grub_minix_dir (grub_device_t device, const char *path,
  476. grub_fs_dir_hook_t hook, void *hook_data)
  477. {
  478. struct grub_minix_data *data = 0;
  479. unsigned int pos = 0;
  480. data = grub_minix_mount (device->disk);
  481. if (!data)
  482. return grub_errno;
  483. grub_minix_read_inode (data, GRUB_MINIX_ROOT_INODE);
  484. if (grub_errno)
  485. goto fail;
  486. grub_minix_find_file (data, path);
  487. if (grub_errno)
  488. goto fail;
  489. if ((GRUB_MINIX_INODE_MODE (data) & GRUB_MINIX_IFDIR) != GRUB_MINIX_IFDIR)
  490. {
  491. grub_error (GRUB_ERR_BAD_FILE_TYPE, N_("not a directory"));
  492. goto fail;
  493. }
  494. while (pos < GRUB_MINIX_INODE_SIZE (data))
  495. {
  496. grub_minix_ino_t ino;
  497. char filename[MAX_MINIX_FILENAME_SIZE + 1];
  498. grub_minix_ino_t dirino = data->ino;
  499. struct grub_dirhook_info info;
  500. grub_memset (&info, 0, sizeof (info));
  501. if (grub_minix_read_file (data, 0, 0, pos, sizeof (ino),
  502. (char *) &ino) < 0)
  503. return grub_errno;
  504. if (grub_minix_read_file (data, 0, 0, pos + sizeof (ino),
  505. data->filename_size,
  506. (char *) filename) < 0)
  507. return grub_errno;
  508. filename[data->filename_size] = '\0';
  509. if (!ino)
  510. {
  511. pos += sizeof (ino) + data->filename_size;
  512. continue;
  513. }
  514. grub_minix_read_inode (data, grub_minix_to_cpu_ino (ino));
  515. info.dir = ((GRUB_MINIX_INODE_MODE (data)
  516. & GRUB_MINIX_IFDIR) == GRUB_MINIX_IFDIR);
  517. info.mtimeset = 1;
  518. info.mtime = grub_minix_to_cpu32 (data->inode.mtime);
  519. if (hook (filename, &info, hook_data) ? 1 : 0)
  520. break;
  521. /* Load the old inode back in. */
  522. grub_minix_read_inode (data, dirino);
  523. pos += sizeof (ino) + data->filename_size;
  524. }
  525. fail:
  526. grub_free (data);
  527. return grub_errno;
  528. }
  529. /* Open a file named NAME and initialize FILE. */
  530. static grub_err_t
  531. grub_minix_open (struct grub_file *file, const char *name)
  532. {
  533. struct grub_minix_data *data;
  534. data = grub_minix_mount (file->device->disk);
  535. if (!data)
  536. return grub_errno;
  537. /* Open the inode op the root directory. */
  538. grub_minix_read_inode (data, GRUB_MINIX_ROOT_INODE);
  539. if (grub_errno)
  540. {
  541. grub_free (data);
  542. return grub_errno;
  543. }
  544. if (!name || name[0] != '/')
  545. {
  546. grub_error (GRUB_ERR_BAD_FILENAME, N_("invalid file name `%s'"), name);
  547. return grub_errno;
  548. }
  549. /* Traverse the directory tree to the node that should be
  550. opened. */
  551. grub_minix_find_file (data, name);
  552. if (grub_errno)
  553. {
  554. grub_free (data);
  555. return grub_errno;
  556. }
  557. file->data = data;
  558. file->size = GRUB_MINIX_INODE_SIZE (data);
  559. return GRUB_ERR_NONE;
  560. }
  561. static grub_ssize_t
  562. grub_minix_read (grub_file_t file, char *buf, grub_size_t len)
  563. {
  564. struct grub_minix_data *data =
  565. (struct grub_minix_data *) file->data;
  566. return grub_minix_read_file (data, file->read_hook, file->read_hook_data,
  567. file->offset, len, buf);
  568. }
  569. static grub_err_t
  570. grub_minix_close (grub_file_t file)
  571. {
  572. grub_free (file->data);
  573. return GRUB_ERR_NONE;
  574. }
  575. static struct grub_fs grub_minix_fs =
  576. {
  577. #ifdef MODE_BIGENDIAN
  578. #if defined(MODE_MINIX3)
  579. .name = "minix3_be",
  580. #elif defined(MODE_MINIX2)
  581. .name = "minix2_be",
  582. #else
  583. .name = "minix_be",
  584. #endif
  585. #else
  586. #if defined(MODE_MINIX3)
  587. .name = "minix3",
  588. #elif defined(MODE_MINIX2)
  589. .name = "minix2",
  590. #else
  591. .name = "minix",
  592. #endif
  593. #endif
  594. .fs_dir = grub_minix_dir,
  595. .fs_open = grub_minix_open,
  596. .fs_read = grub_minix_read,
  597. .fs_close = grub_minix_close,
  598. #ifdef GRUB_UTIL
  599. .reserved_first_sector = 1,
  600. .blocklist_install = 1,
  601. #endif
  602. .next = 0
  603. };
  604. #ifdef MODE_BIGENDIAN
  605. #if defined(MODE_MINIX3)
  606. GRUB_MOD_INIT(minix3_be)
  607. #elif defined(MODE_MINIX2)
  608. GRUB_MOD_INIT(minix2_be)
  609. #else
  610. GRUB_MOD_INIT(minix_be)
  611. #endif
  612. #else
  613. #if defined(MODE_MINIX3)
  614. GRUB_MOD_INIT(minix3)
  615. #elif defined(MODE_MINIX2)
  616. GRUB_MOD_INIT(minix2)
  617. #else
  618. GRUB_MOD_INIT(minix)
  619. #endif
  620. #endif
  621. {
  622. if (!grub_is_lockdown ())
  623. {
  624. grub_minix_fs.mod = mod;
  625. grub_fs_register (&grub_minix_fs);
  626. }
  627. my_mod = mod;
  628. }
  629. #ifdef MODE_BIGENDIAN
  630. #if defined(MODE_MINIX3)
  631. GRUB_MOD_FINI(minix3_be)
  632. #elif defined(MODE_MINIX2)
  633. GRUB_MOD_FINI(minix2_be)
  634. #else
  635. GRUB_MOD_FINI(minix_be)
  636. #endif
  637. #else
  638. #if defined(MODE_MINIX3)
  639. GRUB_MOD_FINI(minix3)
  640. #elif defined(MODE_MINIX2)
  641. GRUB_MOD_FINI(minix2)
  642. #else
  643. GRUB_MOD_FINI(minix)
  644. #endif
  645. #endif
  646. {
  647. if (!grub_is_lockdown ())
  648. grub_fs_unregister (&grub_minix_fs);
  649. }