minix.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  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. #define GRUB_MINIX_MAGIC 0x137F
  27. #define GRUB_MINIX2_MAGIC 0x2468
  28. #define GRUB_MINIX_MAGIC_30 0x138F
  29. #define GRUB_MINIX2_MAGIC_30 0x2478
  30. #define GRUB_MINIX_BSIZE 1024U
  31. #define GRUB_MINIX_LOG2_BSIZE 1
  32. #define GRUB_MINIX_ROOT_INODE 1
  33. #define GRUB_MINIX_MAX_SYMLNK_CNT 8
  34. #define GRUB_MINIX_SBLOCK 2
  35. #define GRUB_MINIX_IFDIR 0040000U
  36. #define GRUB_MINIX_IFLNK 0120000U
  37. #define GRUB_MINIX_INODE(data,field) (data->version == 1 ? \
  38. data->inode. field : data->inode2. field)
  39. #define GRUB_MINIX_INODE_ENDIAN(data,field,bits1,bits2) (data->version == 1 ? \
  40. grub_le_to_cpu##bits1 (data->inode.field) : \
  41. grub_le_to_cpu##bits2 (data->inode2.field))
  42. #define GRUB_MINIX_INODE_SIZE(data) GRUB_MINIX_INODE_ENDIAN (data,size,16,32)
  43. #define GRUB_MINIX_INODE_MODE(data) GRUB_MINIX_INODE_ENDIAN (data,mode,16,16)
  44. #define GRUB_MINIX_INODE_DIR_ZONES(data,blk) GRUB_MINIX_INODE_ENDIAN \
  45. (data,dir_zones[blk],16,32)
  46. #define GRUB_MINIX_INODE_INDIR_ZONE(data) \
  47. GRUB_MINIX_INODE_ENDIAN (data,indir_zone,16,32)
  48. #define GRUB_MINIX_INODE_DINDIR_ZONE(data) \
  49. GRUB_MINIX_INODE_ENDIAN (data,double_indir_zone,16,32)
  50. #define GRUB_MINIX_INODE_BLKSZ(data) (data->version == 1 ? 2 : 4)
  51. #define GRUB_MINIX_LOG2_ZONESZ (GRUB_MINIX_LOG2_BSIZE \
  52. + grub_le_to_cpu16 (sblock->log2_zone_size))
  53. #define GRUB_MINIX_ZONESZ (GRUB_MINIX_BSIZE \
  54. << grub_le_to_cpu16 (sblock->log2_zone_size))
  55. struct grub_minix_sblock
  56. {
  57. grub_uint16_t inode_cnt;
  58. grub_uint16_t zone_cnt;
  59. grub_uint16_t inode_bmap_size;
  60. grub_uint16_t zone_bmap_size;
  61. grub_uint16_t first_data_zone;
  62. grub_uint16_t log2_zone_size;
  63. grub_uint32_t max_file_size;
  64. grub_uint16_t magic;
  65. };
  66. struct grub_minix_inode
  67. {
  68. grub_uint16_t mode;
  69. grub_uint16_t uid;
  70. grub_uint16_t size;
  71. grub_uint32_t ctime;
  72. grub_uint8_t gid;
  73. grub_uint8_t nlinks;
  74. grub_uint16_t dir_zones[7];
  75. grub_uint16_t indir_zone;
  76. grub_uint16_t double_indir_zone;
  77. };
  78. struct grub_minix2_inode
  79. {
  80. grub_uint16_t mode;
  81. grub_uint16_t nlinks;
  82. grub_uint16_t uid;
  83. grub_uint16_t gid;
  84. grub_uint32_t size;
  85. grub_uint32_t atime;
  86. grub_uint32_t mtime;
  87. grub_uint32_t ctime;
  88. grub_uint32_t dir_zones[7];
  89. grub_uint32_t indir_zone;
  90. grub_uint32_t double_indir_zone;
  91. grub_uint32_t unused;
  92. };
  93. /* Information about a "mounted" minix filesystem. */
  94. struct grub_minix_data
  95. {
  96. struct grub_minix_sblock sblock;
  97. struct grub_minix_inode inode;
  98. struct grub_minix2_inode inode2;
  99. int ino;
  100. int linknest;
  101. grub_disk_t disk;
  102. int version;
  103. int filename_size;
  104. };
  105. static grub_dl_t my_mod;
  106. static grub_err_t grub_minix_find_file (struct grub_minix_data *data,
  107. const char *path);
  108. /* Read the block pointer in ZONE, on the offset NUM. */
  109. static int
  110. grub_get_indir (int zone, int num, struct grub_minix_data *data)
  111. {
  112. struct grub_minix_sblock *sblock = &data->sblock;
  113. if (data->version == 1)
  114. {
  115. grub_uint16_t indir16;
  116. grub_disk_read (data->disk,
  117. zone << GRUB_MINIX_LOG2_ZONESZ,
  118. sizeof (grub_uint16_t) * num,
  119. sizeof (grub_uint16_t), (char *) &indir16);
  120. return grub_le_to_cpu16 (indir16);
  121. }
  122. else
  123. {
  124. grub_uint32_t indir32;
  125. grub_disk_read (data->disk,
  126. zone << GRUB_MINIX_LOG2_ZONESZ,
  127. sizeof (grub_uint32_t) * num,
  128. sizeof (grub_uint32_t), (char *) &indir32);
  129. return grub_le_to_cpu32 (indir32);
  130. }
  131. }
  132. static int
  133. grub_minix_get_file_block (struct grub_minix_data *data, unsigned int blk)
  134. {
  135. struct grub_minix_sblock *sblock = &data->sblock;
  136. int indir;
  137. /* Direct block. */
  138. if (blk < 7)
  139. return GRUB_MINIX_INODE_DIR_ZONES (data, blk);
  140. /* Indirect block. */
  141. blk -= 7;
  142. if (blk < GRUB_MINIX_ZONESZ / GRUB_MINIX_INODE_BLKSZ (data))
  143. {
  144. indir = grub_get_indir (GRUB_MINIX_INODE_INDIR_ZONE (data), blk, data);
  145. return indir;
  146. }
  147. /* Double indirect block. */
  148. blk -= GRUB_MINIX_ZONESZ / GRUB_MINIX_INODE_BLKSZ (data);
  149. if (blk < (GRUB_MINIX_ZONESZ / GRUB_MINIX_INODE_BLKSZ (data))
  150. * (GRUB_MINIX_ZONESZ / GRUB_MINIX_INODE_BLKSZ (data)))
  151. {
  152. indir = grub_get_indir (GRUB_MINIX_INODE_DINDIR_ZONE (data),
  153. blk / GRUB_MINIX_ZONESZ, data);
  154. indir = grub_get_indir (indir, blk % GRUB_MINIX_ZONESZ, data);
  155. return indir;
  156. }
  157. /* This should never happen. */
  158. grub_error (GRUB_ERR_OUT_OF_RANGE, "file bigger than maximum size");
  159. return 0;
  160. }
  161. /* Read LEN bytes from the file described by DATA starting with byte
  162. POS. Return the amount of read bytes in READ. */
  163. static grub_ssize_t
  164. grub_minix_read_file (struct grub_minix_data *data,
  165. void (*read_hook) (grub_disk_addr_t sector,
  166. unsigned offset, unsigned length,
  167. void *closure),
  168. void *closure,
  169. int pos, grub_disk_addr_t len, char *buf)
  170. {
  171. struct grub_minix_sblock *sblock = &data->sblock;
  172. int i;
  173. int blockcnt;
  174. /* Adjust len so it we can't read past the end of the file. */
  175. if (len + pos > GRUB_MINIX_INODE_SIZE (data))
  176. len = GRUB_MINIX_INODE_SIZE (data) - pos;
  177. blockcnt = (len + pos + GRUB_MINIX_BSIZE - 1) / GRUB_MINIX_BSIZE;
  178. for (i = pos / GRUB_MINIX_BSIZE; i < blockcnt; i++)
  179. {
  180. int blknr;
  181. int blockoff = pos % GRUB_MINIX_BSIZE;
  182. int blockend = GRUB_MINIX_BSIZE;
  183. int skipfirst = 0;
  184. blknr = grub_minix_get_file_block (data, i);
  185. if (grub_errno)
  186. return -1;
  187. /* Last block. */
  188. if (i == blockcnt - 1)
  189. {
  190. blockend = (len + pos) % GRUB_MINIX_BSIZE;
  191. if (!blockend)
  192. blockend = GRUB_MINIX_BSIZE;
  193. }
  194. /* First block. */
  195. if (i == (pos / (int) GRUB_MINIX_BSIZE))
  196. {
  197. skipfirst = blockoff;
  198. blockend -= skipfirst;
  199. }
  200. data->disk->read_hook = read_hook;
  201. data->disk->closure = closure;
  202. grub_disk_read (data->disk, blknr << GRUB_MINIX_LOG2_ZONESZ,
  203. skipfirst, blockend, buf);
  204. data->disk->read_hook = 0;
  205. if (grub_errno)
  206. return -1;
  207. buf += GRUB_MINIX_BSIZE - skipfirst;
  208. }
  209. return len;
  210. }
  211. /* Read inode INO from the mounted filesystem described by DATA. This
  212. inode is used by default now. */
  213. static grub_err_t
  214. grub_minix_read_inode (struct grub_minix_data *data, int ino)
  215. {
  216. struct grub_minix_sblock *sblock = &data->sblock;
  217. /* Block in which the inode is stored. */
  218. int block;
  219. data->ino = ino;
  220. /* The first inode in minix is inode 1. */
  221. ino--;
  222. block = ((2 + grub_le_to_cpu16 (sblock->inode_bmap_size)
  223. + grub_le_to_cpu16 (sblock->zone_bmap_size))
  224. << GRUB_MINIX_LOG2_BSIZE);
  225. if (data->version == 1)
  226. {
  227. block += ino / (GRUB_DISK_SECTOR_SIZE / sizeof (struct grub_minix_inode));
  228. int offs = (ino % (GRUB_DISK_SECTOR_SIZE
  229. / sizeof (struct grub_minix_inode))
  230. * sizeof (struct grub_minix_inode));
  231. grub_disk_read (data->disk, block, offs,
  232. sizeof (struct grub_minix_inode), &data->inode);
  233. }
  234. else
  235. {
  236. block += ino / (GRUB_DISK_SECTOR_SIZE
  237. / sizeof (struct grub_minix2_inode));
  238. int offs = (ino
  239. % (GRUB_DISK_SECTOR_SIZE / sizeof (struct grub_minix2_inode))
  240. * sizeof (struct grub_minix2_inode));
  241. grub_disk_read (data->disk, block, offs,
  242. sizeof (struct grub_minix2_inode),&data->inode2);
  243. }
  244. return GRUB_ERR_NONE;
  245. }
  246. /* Lookup the symlink the current inode points to. INO is the inode
  247. number of the directory the symlink is relative to. */
  248. static grub_err_t
  249. grub_minix_lookup_symlink (struct grub_minix_data *data, int ino)
  250. {
  251. char symlink[GRUB_MINIX_INODE_SIZE (data) + 1];
  252. if (++data->linknest > GRUB_MINIX_MAX_SYMLNK_CNT)
  253. return grub_error (GRUB_ERR_SYMLINK_LOOP, "too deep nesting of symlinks");
  254. if (grub_minix_read_file (data, 0, 0, 0,
  255. GRUB_MINIX_INODE_SIZE (data), symlink) < 0)
  256. return grub_errno;
  257. symlink[GRUB_MINIX_INODE_SIZE (data)] = '\0';
  258. /* The symlink is an absolute path, go back to the root inode. */
  259. if (symlink[0] == '/')
  260. ino = GRUB_MINIX_ROOT_INODE;
  261. /* Now load in the old inode. */
  262. if (grub_minix_read_inode (data, ino))
  263. return grub_errno;
  264. grub_minix_find_file (data, symlink);
  265. if (grub_errno)
  266. grub_error (grub_errno, "cannot follow symlink `%s'", symlink);
  267. return grub_errno;
  268. }
  269. /* Find the file with the pathname PATH on the filesystem described by
  270. DATA. */
  271. static grub_err_t
  272. grub_minix_find_file (struct grub_minix_data *data, const char *path)
  273. {
  274. char fpath[grub_strlen (path) + 1];
  275. char *name = fpath;
  276. char *next;
  277. unsigned int pos = 0;
  278. int dirino;
  279. grub_strcpy (fpath, path);
  280. /* Skip the first slash. */
  281. if (name[0] == '/')
  282. {
  283. name++;
  284. if (!*name)
  285. return 0;
  286. }
  287. /* Extract the actual part from the pathname. */
  288. next = grub_strchr (name, '/');
  289. if (next)
  290. {
  291. next[0] = '\0';
  292. next++;
  293. }
  294. do
  295. {
  296. grub_uint16_t ino;
  297. char filename[data->filename_size + 1];
  298. if (grub_strlen (name) == 0)
  299. return GRUB_ERR_NONE;
  300. if (grub_minix_read_file (data, 0, 0, pos, sizeof (ino),
  301. (char *) &ino) < 0)
  302. return grub_errno;
  303. if (grub_minix_read_file (data, 0, 0, pos + sizeof (ino),
  304. data->filename_size, (char *) filename)< 0)
  305. return grub_errno;
  306. filename[data->filename_size] = '\0';
  307. /* Check if the current direntry matches the current part of the
  308. pathname. */
  309. if (!grub_strcmp (name, filename))
  310. {
  311. dirino = data->ino;
  312. grub_minix_read_inode (data, grub_le_to_cpu16 (ino));
  313. /* Follow the symlink. */
  314. if ((GRUB_MINIX_INODE_MODE (data)
  315. & GRUB_MINIX_IFLNK) == GRUB_MINIX_IFLNK)
  316. {
  317. grub_minix_lookup_symlink (data, dirino);
  318. if (grub_errno)
  319. return grub_errno;
  320. }
  321. if (!next)
  322. return 0;
  323. pos = 0;
  324. name = next;
  325. next = grub_strchr (name, '/');
  326. if (next)
  327. {
  328. next[0] = '\0';
  329. next++;
  330. }
  331. if ((GRUB_MINIX_INODE_MODE (data)
  332. & GRUB_MINIX_IFDIR) != GRUB_MINIX_IFDIR)
  333. return grub_error (GRUB_ERR_BAD_FILE_TYPE, "not a directory");
  334. continue;
  335. }
  336. pos += sizeof (ino) + data->filename_size;
  337. } while (pos < GRUB_MINIX_INODE_SIZE (data));
  338. grub_error (GRUB_ERR_FILE_NOT_FOUND, "file not found");
  339. return grub_errno;
  340. }
  341. /* Mount the filesystem on the disk DISK. */
  342. static struct grub_minix_data *
  343. grub_minix_mount (grub_disk_t disk)
  344. {
  345. struct grub_minix_data *data;
  346. data = grub_malloc (sizeof (struct grub_minix_data));
  347. if (!data)
  348. return 0;
  349. /* Read the superblock. */
  350. grub_disk_read (disk, GRUB_MINIX_SBLOCK, 0,
  351. sizeof (struct grub_minix_sblock),&data->sblock);
  352. if (grub_errno)
  353. goto fail;
  354. if (grub_le_to_cpu16 (data->sblock.magic) == GRUB_MINIX_MAGIC)
  355. {
  356. data->version = 1;
  357. data->filename_size = 14;
  358. }
  359. else if (grub_le_to_cpu16 (data->sblock.magic) == GRUB_MINIX2_MAGIC)
  360. {
  361. data->version = 2;
  362. data->filename_size = 14;
  363. }
  364. else if (grub_le_to_cpu16 (data->sblock.magic) == GRUB_MINIX_MAGIC_30)
  365. {
  366. data->version = 1;
  367. data->filename_size = 30;
  368. }
  369. else if (grub_le_to_cpu16 (data->sblock.magic) == GRUB_MINIX2_MAGIC_30)
  370. {
  371. data->version = 2;
  372. data->filename_size = 30;
  373. }
  374. else
  375. goto fail;
  376. data->disk = disk;
  377. data->linknest = 0;
  378. return data;
  379. fail:
  380. grub_free (data);
  381. grub_error (GRUB_ERR_BAD_FS, "not a minix filesystem");
  382. return 0;
  383. }
  384. static grub_err_t
  385. grub_minix_dir (grub_device_t device, const char *path,
  386. int (*hook) (const char *filename,
  387. const struct grub_dirhook_info *info,
  388. void *closure),
  389. void *closure)
  390. {
  391. struct grub_minix_data *data = 0;
  392. struct grub_minix_sblock *sblock;
  393. unsigned int pos = 0;
  394. data = grub_minix_mount (device->disk);
  395. if (!data)
  396. return grub_errno;
  397. grub_minix_read_inode (data, GRUB_MINIX_ROOT_INODE);
  398. if (grub_errno)
  399. goto fail;
  400. sblock = &data->sblock;
  401. grub_minix_find_file (data, path);
  402. if (grub_errno)
  403. goto fail;
  404. if ((GRUB_MINIX_INODE_MODE (data) & GRUB_MINIX_IFDIR) != GRUB_MINIX_IFDIR)
  405. {
  406. grub_error (GRUB_ERR_BAD_FILE_TYPE, "not a directory");
  407. goto fail;
  408. }
  409. while (pos < GRUB_MINIX_INODE_SIZE (data))
  410. {
  411. grub_uint16_t ino;
  412. char filename[data->filename_size + 1];
  413. int dirino = data->ino;
  414. struct grub_dirhook_info info;
  415. grub_memset (&info, 0, sizeof (info));
  416. if (grub_minix_read_file (data, 0, 0, pos, sizeof (ino),
  417. (char *) &ino) < 0)
  418. return grub_errno;
  419. if (grub_minix_read_file (data, 0, 0, pos + sizeof (ino),
  420. data->filename_size,
  421. (char *) filename) < 0)
  422. return grub_errno;
  423. filename[data->filename_size] = '\0';
  424. /* The filetype is not stored in the dirent. Read the inode to
  425. find out the filetype. This *REALLY* sucks. */
  426. grub_minix_read_inode (data, grub_le_to_cpu16 (ino));
  427. info.dir = ((GRUB_MINIX_INODE_MODE (data)
  428. & GRUB_MINIX_IFDIR) == GRUB_MINIX_IFDIR);
  429. if (hook (filename, &info, closure))
  430. break;
  431. /* Load the old inode back in. */
  432. grub_minix_read_inode (data, dirino);
  433. pos += sizeof (ino) + data->filename_size;
  434. }
  435. fail:
  436. grub_free (data);
  437. return grub_errno;
  438. }
  439. /* Open a file named NAME and initialize FILE. */
  440. static grub_err_t
  441. grub_minix_open (struct grub_file *file, const char *name)
  442. {
  443. struct grub_minix_data *data;
  444. data = grub_minix_mount (file->device->disk);
  445. if (!data)
  446. return grub_errno;
  447. /* Open the inode op the root directory. */
  448. grub_minix_read_inode (data, GRUB_MINIX_ROOT_INODE);
  449. if (grub_errno)
  450. {
  451. grub_free (data);
  452. return grub_errno;
  453. }
  454. if (!name || name[0] != '/')
  455. {
  456. grub_error (GRUB_ERR_BAD_FILENAME, "bad filename");
  457. return grub_errno;
  458. }
  459. /* Traverse the directory tree to the node that should be
  460. opened. */
  461. grub_minix_find_file (data, name);
  462. if (grub_errno)
  463. {
  464. grub_free (data);
  465. return grub_errno;
  466. }
  467. file->data = data;
  468. file->size = GRUB_MINIX_INODE_SIZE (data);
  469. return GRUB_ERR_NONE;
  470. }
  471. static grub_ssize_t
  472. grub_minix_read (grub_file_t file, char *buf, grub_size_t len)
  473. {
  474. struct grub_minix_data *data =
  475. (struct grub_minix_data *) file->data;
  476. return grub_minix_read_file (data, file->read_hook, file->closure,
  477. file->offset, len, buf);
  478. }
  479. static grub_err_t
  480. grub_minix_close (grub_file_t file)
  481. {
  482. grub_free (file->data);
  483. return GRUB_ERR_NONE;
  484. }
  485. static grub_err_t
  486. grub_minix_label (grub_device_t device __attribute ((unused)),
  487. char **label __attribute ((unused)))
  488. {
  489. return GRUB_ERR_NONE;
  490. }
  491. static struct grub_fs grub_minix_fs =
  492. {
  493. .name = "minix",
  494. .dir = grub_minix_dir,
  495. .open = grub_minix_open,
  496. .read = grub_minix_read,
  497. .close = grub_minix_close,
  498. .label = grub_minix_label,
  499. .next = 0
  500. };
  501. GRUB_MOD_INIT(minix)
  502. {
  503. grub_fs_register (&grub_minix_fs);
  504. my_mod = mod;
  505. }
  506. GRUB_MOD_FINI(minix)
  507. {
  508. grub_fs_unregister (&grub_minix_fs);
  509. }