sfs.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  1. /* sfs.c - Amiga Smart FileSystem. */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 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/fshelp.h>
  27. /* The common header for a block. */
  28. struct grub_sfs_bheader
  29. {
  30. grub_uint8_t magic[4];
  31. grub_uint32_t chksum;
  32. grub_uint32_t ipointtomyself;
  33. } __attribute__ ((packed));
  34. /* The sfs rootblock. */
  35. struct grub_sfs_rblock
  36. {
  37. struct grub_sfs_bheader header;
  38. grub_uint32_t version;
  39. grub_uint8_t unused1[36];
  40. grub_uint32_t blocksize;
  41. grub_uint8_t unused2[40];
  42. grub_uint8_t unused3[8];
  43. grub_uint32_t rootobject;
  44. grub_uint32_t btree;
  45. } __attribute__ ((packed));
  46. /* A SFS object container. */
  47. struct grub_sfs_obj
  48. {
  49. grub_uint8_t unused1[4];
  50. grub_uint32_t nodeid;
  51. grub_uint8_t unused2[4];
  52. union
  53. {
  54. struct
  55. {
  56. grub_uint32_t first_block;
  57. grub_uint32_t size;
  58. } file __attribute__ ((packed));
  59. struct
  60. {
  61. grub_uint32_t hashtable;
  62. grub_uint32_t dir_objc;
  63. } dir __attribute__ ((packed));
  64. } file_dir;
  65. grub_uint8_t unused3[4];
  66. grub_uint8_t type;
  67. grub_uint8_t filename[1];
  68. grub_uint8_t comment[1];
  69. } __attribute__ ((packed));
  70. #define GRUB_SFS_TYPE_DELETED 32
  71. #define GRUB_SFS_TYPE_SYMLINK 64
  72. #define GRUB_SFS_TYPE_DIR 128
  73. /* A SFS object container. */
  74. struct grub_sfs_objc
  75. {
  76. struct grub_sfs_bheader header;
  77. grub_uint32_t parent;
  78. grub_uint32_t next;
  79. grub_uint32_t prev;
  80. /* The amount of objects depends on the blocksize. */
  81. struct grub_sfs_obj objects[1];
  82. } __attribute__ ((packed));
  83. struct grub_sfs_btree_node
  84. {
  85. grub_uint32_t key;
  86. grub_uint32_t data;
  87. } __attribute__ ((packed));
  88. struct grub_sfs_btree_extent
  89. {
  90. grub_uint32_t key;
  91. grub_uint32_t next;
  92. grub_uint32_t prev;
  93. grub_uint16_t size;
  94. } __attribute__ ((packed));
  95. struct grub_sfs_btree
  96. {
  97. struct grub_sfs_bheader header;
  98. grub_uint16_t nodes;
  99. grub_uint8_t leaf;
  100. grub_uint8_t nodesize;
  101. /* Normally this can be kind of node, but just extents are
  102. supported. */
  103. struct grub_sfs_btree_node node[1];
  104. } __attribute__ ((packed));
  105. struct grub_fshelp_node
  106. {
  107. struct grub_sfs_data *data;
  108. int block;
  109. int size;
  110. };
  111. /* Information about a "mounted" sfs filesystem. */
  112. struct grub_sfs_data
  113. {
  114. struct grub_sfs_rblock rblock;
  115. struct grub_fshelp_node diropen;
  116. grub_disk_t disk;
  117. /* Blocksize in sectors. */
  118. unsigned int blocksize;
  119. /* Label of the filesystem. */
  120. char *label;
  121. };
  122. static grub_dl_t my_mod;
  123. /* Lookup the extent starting with BLOCK in the filesystem described
  124. by DATA. Return the extent size in SIZE and the following extent
  125. in NEXTEXT. */
  126. static grub_err_t
  127. grub_sfs_read_extent (struct grub_sfs_data *data, unsigned int block,
  128. int *size, int *nextext)
  129. {
  130. char *treeblock;
  131. struct grub_sfs_btree *tree;
  132. int i;
  133. int next;
  134. int prev;
  135. treeblock = grub_malloc (data->blocksize);
  136. if (!block)
  137. return 0;
  138. next = grub_be_to_cpu32 (data->rblock.btree);
  139. tree = (struct grub_sfs_btree *) treeblock;
  140. /* Handle this level in the btree. */
  141. do
  142. {
  143. prev = 0;
  144. grub_disk_read (data->disk, next, 0, data->blocksize, treeblock);
  145. if (grub_errno)
  146. {
  147. grub_free (treeblock);
  148. return grub_errno;
  149. }
  150. for (i = grub_be_to_cpu16 (tree->nodes) - 1; i >= 0; i--)
  151. {
  152. #define EXTNODE(tree, index) \
  153. ((struct grub_sfs_btree_node *) (((char *) &(tree)->node[0]) \
  154. + (index) * (tree)->nodesize))
  155. /* Follow the tree down to the leaf level. */
  156. if ((grub_be_to_cpu32 (EXTNODE(tree, i)->key) <= block)
  157. && !tree->leaf)
  158. {
  159. next = grub_be_to_cpu32 (EXTNODE (tree, i)->data);
  160. break;
  161. }
  162. /* If the leaf level is reached, just find the correct extent. */
  163. if (grub_be_to_cpu32 (EXTNODE (tree, i)->key) == block && tree->leaf)
  164. {
  165. struct grub_sfs_btree_extent *extent;
  166. extent = (struct grub_sfs_btree_extent *) EXTNODE (tree, i);
  167. /* We found a correct leaf. */
  168. *size = grub_be_to_cpu16 (extent->size);
  169. *nextext = grub_be_to_cpu32 (extent->next);
  170. grub_free (treeblock);
  171. return 0;
  172. }
  173. #undef EXTNODE
  174. }
  175. } while (!tree->leaf);
  176. grub_free (treeblock);
  177. return grub_error (GRUB_ERR_FILE_READ_ERROR, "SFS extent not found");
  178. }
  179. static grub_disk_addr_t
  180. grub_sfs_read_block (grub_fshelp_node_t node, grub_disk_addr_t fileblock)
  181. {
  182. int blk = node->block;
  183. int size = 0;
  184. int next = 0;
  185. while (blk)
  186. {
  187. grub_err_t err;
  188. /* In case of the first block we don't have to lookup the
  189. extent, the minimum size is always 1. */
  190. if (fileblock == 0)
  191. return blk;
  192. err = grub_sfs_read_extent (node->data, blk, &size, &next);
  193. if (err)
  194. return 0;
  195. if (fileblock < (unsigned int) size)
  196. return fileblock + blk;
  197. fileblock -= size;
  198. blk = next;
  199. }
  200. grub_error (GRUB_ERR_FILE_READ_ERROR,
  201. "reading a SFS block outside the extent");
  202. return 0;
  203. }
  204. /* Read LEN bytes from the file described by DATA starting with byte
  205. POS. Return the amount of read bytes in READ. */
  206. static grub_ssize_t
  207. grub_sfs_read_file (grub_fshelp_node_t node,
  208. void (*read_hook) (grub_disk_addr_t sector,
  209. unsigned offset, unsigned length,
  210. void *closure),
  211. void *closure, int flags,
  212. int pos, grub_size_t len, char *buf)
  213. {
  214. return grub_fshelp_read_file (node->data->disk, node, read_hook, closure,
  215. flags, pos, len, buf, grub_sfs_read_block,
  216. node->size, 0);
  217. }
  218. static struct grub_sfs_data *
  219. grub_sfs_mount (grub_disk_t disk)
  220. {
  221. struct grub_sfs_data *data;
  222. struct grub_sfs_objc *rootobjc;
  223. char *rootobjc_data = 0;
  224. unsigned int blk;
  225. data = grub_malloc (sizeof (*data));
  226. if (!data)
  227. return 0;
  228. /* Read the rootblock. */
  229. grub_disk_read (disk, 0, 0, sizeof (struct grub_sfs_rblock),
  230. &data->rblock);
  231. if (grub_errno)
  232. goto fail;
  233. /* Make sure this is a sfs filesystem. */
  234. if (grub_strncmp ((char *) (data->rblock.header.magic), "SFS", 4))
  235. {
  236. grub_error (GRUB_ERR_BAD_FS, "not a SFS filesystem");
  237. goto fail;
  238. }
  239. data->blocksize = grub_be_to_cpu32 (data->rblock.blocksize);
  240. rootobjc_data = grub_malloc (data->blocksize);
  241. if (! rootobjc_data)
  242. goto fail;
  243. /* Read the root object container. */
  244. grub_disk_read (disk, grub_be_to_cpu32 (data->rblock.rootobject), 0,
  245. data->blocksize, rootobjc_data);
  246. if (grub_errno)
  247. goto fail;
  248. rootobjc = (struct grub_sfs_objc *) rootobjc_data;
  249. blk = grub_be_to_cpu32 (rootobjc->objects[0].file_dir.dir.dir_objc);
  250. data->diropen.size = 0;
  251. data->diropen.block = blk;
  252. data->diropen.data = data;
  253. data->disk = disk;
  254. data->label = grub_strdup ((char *) (rootobjc->objects[0].filename));
  255. return data;
  256. fail:
  257. if (grub_errno == GRUB_ERR_OUT_OF_RANGE)
  258. grub_error (GRUB_ERR_BAD_FS, "not an SFS filesystem");
  259. grub_free (data);
  260. grub_free (rootobjc_data);
  261. return 0;
  262. }
  263. static char *
  264. grub_sfs_read_symlink (grub_fshelp_node_t node)
  265. {
  266. struct grub_sfs_data *data = node->data;
  267. char *symlink;
  268. char *block;
  269. block = grub_malloc (data->blocksize);
  270. if (!block)
  271. return 0;
  272. grub_disk_read (data->disk, node->block, 0, data->blocksize, block);
  273. if (grub_errno)
  274. {
  275. grub_free (block);
  276. return 0;
  277. }
  278. /* This is just a wild guess, but it always worked for me. How the
  279. SLNK block looks like is not documented in the SFS docs. */
  280. symlink = grub_strdup (&block[24]);
  281. grub_free (block);
  282. if (!symlink)
  283. return 0;
  284. return symlink;
  285. }
  286. static int
  287. grub_sfs_create_node (const char *name, int block,
  288. int size, int type,
  289. struct grub_sfs_data *data,
  290. int (*hook) (const char *filename,
  291. enum grub_fshelp_filetype filetype,
  292. grub_fshelp_node_t node, void *closure),
  293. void *closure)
  294. {
  295. struct grub_fshelp_node *node;
  296. node = grub_malloc (sizeof (*node));
  297. if (!node)
  298. return 1;
  299. node->data = data;
  300. node->size = size;
  301. node->block = block;
  302. return hook (name, type, node, closure);
  303. }
  304. static int
  305. grub_sfs_iterate_dir (grub_fshelp_node_t dir,
  306. int (*hook) (const char *filename,
  307. enum grub_fshelp_filetype filetype,
  308. grub_fshelp_node_t node, void *closure),
  309. void *closure)
  310. {
  311. struct grub_sfs_data *data = dir->data;
  312. char *objc_data;
  313. struct grub_sfs_objc *objc;
  314. unsigned int next = dir->block;
  315. int pos;
  316. objc_data = grub_malloc (data->blocksize);
  317. if (!objc_data)
  318. goto fail;
  319. /* The Object container can consist of multiple blocks, iterate over
  320. every block. */
  321. while (next)
  322. {
  323. grub_disk_read (data->disk, next, 0, data->blocksize, objc_data);
  324. if (grub_errno)
  325. goto fail;
  326. objc = (struct grub_sfs_objc *) objc_data;
  327. pos = (char *) &objc->objects[0] - (char *) objc;
  328. /* Iterate over all entries in this block. */
  329. while (pos + sizeof (struct grub_sfs_obj) < data->blocksize)
  330. {
  331. struct grub_sfs_obj *obj;
  332. obj = (struct grub_sfs_obj *) ((char *) objc + pos);
  333. char *filename = (char *) (obj->filename);
  334. int len;
  335. enum grub_fshelp_filetype type;
  336. unsigned int block;
  337. /* The filename and comment dynamically increase the size of
  338. the object. */
  339. len = grub_strlen (filename);
  340. len += grub_strlen (filename + len + 1);
  341. pos += sizeof (*obj) + len;
  342. /* Round up to a multiple of two bytes. */
  343. pos = ((pos + 1) >> 1) << 1;
  344. if (grub_strlen (filename) == 0)
  345. continue;
  346. /* First check if the file was not deleted. */
  347. if (obj->type & GRUB_SFS_TYPE_DELETED)
  348. continue;
  349. else if (obj->type & GRUB_SFS_TYPE_SYMLINK)
  350. type = GRUB_FSHELP_SYMLINK;
  351. else if (obj->type & GRUB_SFS_TYPE_DIR)
  352. type = GRUB_FSHELP_DIR;
  353. else
  354. type = GRUB_FSHELP_REG;
  355. if (type == GRUB_FSHELP_DIR)
  356. block = grub_be_to_cpu32 (obj->file_dir.dir.dir_objc);
  357. else
  358. block = grub_be_to_cpu32 (obj->file_dir.file.first_block);
  359. if (grub_sfs_create_node (filename, block,
  360. grub_be_to_cpu32 (obj->file_dir.file.size),
  361. type, data, hook, closure))
  362. {
  363. grub_free (objc_data);
  364. return 1;
  365. }
  366. }
  367. next = grub_be_to_cpu32 (objc->next);
  368. }
  369. fail:
  370. grub_free (objc_data);
  371. return 0;
  372. }
  373. /* Open a file named NAME and initialize FILE. */
  374. static grub_err_t
  375. grub_sfs_open (struct grub_file *file, const char *name)
  376. {
  377. struct grub_sfs_data *data;
  378. struct grub_fshelp_node *fdiro = 0;
  379. grub_dl_ref (my_mod);
  380. data = grub_sfs_mount (file->device->disk);
  381. if (!data)
  382. goto fail;
  383. grub_fshelp_find_file (name, &data->diropen, &fdiro, grub_sfs_iterate_dir, 0,
  384. grub_sfs_read_symlink, GRUB_FSHELP_REG);
  385. if (grub_errno)
  386. goto fail;
  387. file->size = fdiro->size;
  388. data->diropen = *fdiro;
  389. grub_free (fdiro);
  390. file->data = data;
  391. file->offset = 0;
  392. return 0;
  393. fail:
  394. if (data && fdiro != &data->diropen)
  395. grub_free (fdiro);
  396. if (data)
  397. grub_free (data->label);
  398. grub_free (data);
  399. grub_dl_unref (my_mod);
  400. return grub_errno;
  401. }
  402. static grub_err_t
  403. grub_sfs_close (grub_file_t file)
  404. {
  405. grub_free (file->data);
  406. grub_dl_unref (my_mod);
  407. return GRUB_ERR_NONE;
  408. }
  409. /* Read LEN bytes data from FILE into BUF. */
  410. static grub_ssize_t
  411. grub_sfs_read (grub_file_t file, char *buf, grub_size_t len)
  412. {
  413. struct grub_sfs_data *data = (struct grub_sfs_data *) file->data;
  414. int size = grub_sfs_read_file (&data->diropen, file->read_hook,
  415. file->closure, file->flags,
  416. file->offset, len, buf);
  417. return size;
  418. }
  419. struct grub_sfs_dir_closure
  420. {
  421. int (*hook) (const char *filename,
  422. const struct grub_dirhook_info *info,
  423. void *closure);
  424. void *closure;
  425. };
  426. static int
  427. iterate (const char *filename,
  428. enum grub_fshelp_filetype filetype,
  429. grub_fshelp_node_t node,
  430. void *closure)
  431. {
  432. struct grub_sfs_dir_closure *c = closure;
  433. struct grub_dirhook_info info;
  434. grub_memset (&info, 0, sizeof (info));
  435. info.dir = ((filetype & GRUB_FSHELP_TYPE_MASK) == GRUB_FSHELP_DIR);
  436. grub_free (node);
  437. return c->hook (filename, &info, c->closure);
  438. }
  439. static grub_err_t
  440. grub_sfs_dir (grub_device_t device, const char *path,
  441. int (*hook) (const char *filename,
  442. const struct grub_dirhook_info *info,
  443. void *closure),
  444. void *closure)
  445. {
  446. struct grub_sfs_data *data = 0;
  447. struct grub_fshelp_node *fdiro = 0;
  448. struct grub_sfs_dir_closure c;
  449. grub_dl_ref (my_mod);
  450. data = grub_sfs_mount (device->disk);
  451. if (!data)
  452. goto fail;
  453. grub_fshelp_find_file (path, &data->diropen, &fdiro, grub_sfs_iterate_dir, 0,
  454. grub_sfs_read_symlink, GRUB_FSHELP_DIR);
  455. if (grub_errno)
  456. goto fail;
  457. c.hook = hook;
  458. c.closure = closure;
  459. grub_sfs_iterate_dir (fdiro, iterate, &c);
  460. fail:
  461. if (data && fdiro != &data->diropen)
  462. grub_free (fdiro);
  463. if (data)
  464. grub_free (data->label);
  465. grub_free (data);
  466. grub_dl_unref (my_mod);
  467. return grub_errno;
  468. }
  469. static grub_err_t
  470. grub_sfs_label (grub_device_t device, char **label)
  471. {
  472. struct grub_sfs_data *data;
  473. grub_disk_t disk = device->disk;
  474. data = grub_sfs_mount (disk);
  475. if (data)
  476. *label = data->label;
  477. grub_free (data);
  478. return grub_errno;
  479. }
  480. static struct grub_fs grub_sfs_fs =
  481. {
  482. .name = "sfs",
  483. .dir = grub_sfs_dir,
  484. .open = grub_sfs_open,
  485. .read = grub_sfs_read,
  486. .close = grub_sfs_close,
  487. .label = grub_sfs_label,
  488. .next = 0
  489. };
  490. GRUB_MOD_INIT(sfs)
  491. {
  492. grub_fs_register (&grub_sfs_fs);
  493. my_mod = mod;
  494. }
  495. GRUB_MOD_FINI(sfs)
  496. {
  497. grub_fs_unregister (&grub_sfs_fs);
  498. }