cbfs.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. /* cbfs.c - cbfs and tar filesystem. */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2007,2008,2009,2013 Free Software Foundation, Inc.
  5. *
  6. * This program 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. * This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <grub/misc.h>
  20. #include <grub/disk.h>
  21. #include <grub/archelp.h>
  22. #include <grub/file.h>
  23. #include <grub/mm.h>
  24. #include <grub/dl.h>
  25. #include <grub/i18n.h>
  26. #include <grub/cbfs_core.h>
  27. GRUB_MOD_LICENSE ("GPLv3+");
  28. struct grub_archelp_data
  29. {
  30. grub_disk_t disk;
  31. grub_off_t hofs, next_hofs;
  32. grub_off_t dofs;
  33. grub_off_t size;
  34. grub_off_t cbfs_start;
  35. grub_off_t cbfs_end;
  36. grub_off_t cbfs_align;
  37. };
  38. static grub_err_t
  39. grub_cbfs_find_file (struct grub_archelp_data *data, char **name,
  40. grub_int32_t *mtime,
  41. grub_uint32_t *mode)
  42. {
  43. grub_size_t offset;
  44. for (;;
  45. data->dofs = data->hofs + offset,
  46. data->next_hofs = ALIGN_UP (data->dofs + data->size, data->cbfs_align))
  47. {
  48. struct cbfs_file hd;
  49. grub_size_t namesize;
  50. data->hofs = data->next_hofs;
  51. if (data->hofs >= data->cbfs_end)
  52. {
  53. *mode = GRUB_ARCHELP_ATTR_END;
  54. return GRUB_ERR_NONE;
  55. }
  56. if (grub_disk_read (data->disk, 0, data->hofs, sizeof (hd), &hd))
  57. return grub_errno;
  58. if (grub_memcmp (hd.magic, CBFS_FILE_MAGIC, sizeof (hd.magic)) != 0)
  59. {
  60. *mode = GRUB_ARCHELP_ATTR_END;
  61. return GRUB_ERR_NONE;
  62. }
  63. data->size = grub_be_to_cpu32 (hd.len);
  64. (void) mtime;
  65. offset = grub_be_to_cpu32 (hd.offset);
  66. *mode = GRUB_ARCHELP_ATTR_FILE | GRUB_ARCHELP_ATTR_NOTIME;
  67. namesize = offset;
  68. if (namesize >= sizeof (hd))
  69. namesize -= sizeof (hd);
  70. if (namesize == 0)
  71. continue;
  72. *name = grub_malloc (namesize + 1);
  73. if (*name == NULL)
  74. return grub_errno;
  75. if (grub_disk_read (data->disk, 0, data->hofs + sizeof (hd),
  76. namesize, *name))
  77. {
  78. grub_free (*name);
  79. return grub_errno;
  80. }
  81. if ((*name)[0] == '\0')
  82. {
  83. grub_free (*name);
  84. *name = NULL;
  85. continue;
  86. }
  87. (*name)[namesize] = 0;
  88. data->dofs = data->hofs + offset;
  89. data->next_hofs = ALIGN_UP (data->dofs + data->size, data->cbfs_align);
  90. return GRUB_ERR_NONE;
  91. }
  92. }
  93. static void
  94. grub_cbfs_rewind (struct grub_archelp_data *data)
  95. {
  96. data->next_hofs = data->cbfs_start;
  97. }
  98. static struct grub_archelp_ops arcops =
  99. {
  100. .find_file = grub_cbfs_find_file,
  101. .rewind = grub_cbfs_rewind
  102. };
  103. static int
  104. validate_head (struct cbfs_header *head)
  105. {
  106. return (head->magic == grub_cpu_to_be32_compile_time (CBFS_HEADER_MAGIC)
  107. && (head->version
  108. == grub_cpu_to_be32_compile_time (CBFS_HEADER_VERSION1)
  109. || head->version
  110. == grub_cpu_to_be32_compile_time (CBFS_HEADER_VERSION2))
  111. && (grub_be_to_cpu32 (head->bootblocksize)
  112. < grub_be_to_cpu32 (head->romsize))
  113. && (grub_be_to_cpu32 (head->offset)
  114. < grub_be_to_cpu32 (head->romsize))
  115. && (grub_be_to_cpu32 (head->offset)
  116. + grub_be_to_cpu32 (head->bootblocksize)
  117. < grub_be_to_cpu32 (head->romsize))
  118. && head->align != 0
  119. && (head->align & (head->align - 1)) == 0
  120. && head->romsize != 0);
  121. }
  122. static struct grub_archelp_data *
  123. grub_cbfs_mount (grub_disk_t disk)
  124. {
  125. struct cbfs_file hd;
  126. struct grub_archelp_data *data = NULL;
  127. grub_uint32_t ptr;
  128. grub_off_t header_off;
  129. struct cbfs_header head;
  130. if (grub_disk_get_size (disk) == GRUB_DISK_SIZE_UNKNOWN)
  131. goto fail;
  132. if (grub_disk_read (disk, grub_disk_get_size (disk) - 1,
  133. GRUB_DISK_SECTOR_SIZE - sizeof (ptr),
  134. sizeof (ptr), &ptr))
  135. goto fail;
  136. ptr = grub_cpu_to_le32 (ptr);
  137. header_off = (grub_disk_get_size (disk) << GRUB_DISK_SECTOR_BITS)
  138. + (grub_int32_t) ptr;
  139. if (grub_disk_read (disk, 0, header_off,
  140. sizeof (head), &head))
  141. goto fail;
  142. if (!validate_head (&head))
  143. goto fail;
  144. data = (struct grub_archelp_data *) grub_zalloc (sizeof (*data));
  145. if (!data)
  146. goto fail;
  147. data->cbfs_start = (grub_disk_get_size (disk) << GRUB_DISK_SECTOR_BITS)
  148. - (grub_be_to_cpu32 (head.romsize) - grub_be_to_cpu32 (head.offset));
  149. data->cbfs_end = (grub_disk_get_size (disk) << GRUB_DISK_SECTOR_BITS)
  150. - grub_be_to_cpu32 (head.bootblocksize);
  151. data->cbfs_align = grub_be_to_cpu32 (head.align);
  152. if (data->cbfs_start >= (grub_disk_get_size (disk) << GRUB_DISK_SECTOR_BITS))
  153. goto fail;
  154. if (data->cbfs_end > (grub_disk_get_size (disk) << GRUB_DISK_SECTOR_BITS))
  155. data->cbfs_end = (grub_disk_get_size (disk) << GRUB_DISK_SECTOR_BITS);
  156. data->next_hofs = data->cbfs_start;
  157. if (grub_disk_read (disk, 0, data->cbfs_start, sizeof (hd), &hd))
  158. goto fail;
  159. if (grub_memcmp (hd.magic, CBFS_FILE_MAGIC, sizeof (CBFS_FILE_MAGIC) - 1))
  160. goto fail;
  161. data->disk = disk;
  162. return data;
  163. fail:
  164. grub_free (data);
  165. grub_error (GRUB_ERR_BAD_FS, "not a cbfs filesystem");
  166. return 0;
  167. }
  168. static grub_err_t
  169. grub_cbfs_dir (grub_device_t device, const char *path_in,
  170. grub_fs_dir_hook_t hook, void *hook_data)
  171. {
  172. struct grub_archelp_data *data;
  173. grub_err_t err;
  174. data = grub_cbfs_mount (device->disk);
  175. if (!data)
  176. return grub_errno;
  177. err = grub_archelp_dir (data, &arcops,
  178. path_in, hook, hook_data);
  179. grub_free (data);
  180. return err;
  181. }
  182. static grub_err_t
  183. grub_cbfs_open (grub_file_t file, const char *name_in)
  184. {
  185. struct grub_archelp_data *data;
  186. grub_err_t err;
  187. data = grub_cbfs_mount (file->device->disk);
  188. if (!data)
  189. return grub_errno;
  190. err = grub_archelp_open (data, &arcops, name_in);
  191. if (err)
  192. {
  193. grub_free (data);
  194. }
  195. else
  196. {
  197. file->data = data;
  198. file->size = data->size;
  199. }
  200. return err;
  201. }
  202. static grub_ssize_t
  203. grub_cbfs_read (grub_file_t file, char *buf, grub_size_t len)
  204. {
  205. struct grub_archelp_data *data;
  206. grub_ssize_t ret;
  207. data = file->data;
  208. data->disk->read_hook = file->read_hook;
  209. data->disk->read_hook_data = file->read_hook_data;
  210. ret = (grub_disk_read (data->disk, 0, data->dofs + file->offset,
  211. len, buf)) ? -1 : (grub_ssize_t) len;
  212. data->disk->read_hook = 0;
  213. return ret;
  214. }
  215. static grub_err_t
  216. grub_cbfs_close (grub_file_t file)
  217. {
  218. struct grub_archelp_data *data;
  219. data = file->data;
  220. grub_free (data);
  221. return grub_errno;
  222. }
  223. #if (defined (__i386__) || defined (__x86_64__)) && !defined (GRUB_UTIL) \
  224. && !defined (GRUB_MACHINE_EMU) && !defined (GRUB_MACHINE_XEN)
  225. static char *cbfsdisk_addr;
  226. static grub_off_t cbfsdisk_size = 0;
  227. static int
  228. grub_cbfsdisk_iterate (grub_disk_dev_iterate_hook_t hook, void *hook_data,
  229. grub_disk_pull_t pull)
  230. {
  231. if (pull != GRUB_DISK_PULL_NONE)
  232. return 0;
  233. return hook ("cbfsdisk", hook_data);
  234. }
  235. static grub_err_t
  236. grub_cbfsdisk_open (const char *name, grub_disk_t disk)
  237. {
  238. if (grub_strcmp (name, "cbfsdisk"))
  239. return grub_error (GRUB_ERR_UNKNOWN_DEVICE, "not a cbfsdisk");
  240. disk->total_sectors = cbfsdisk_size / GRUB_DISK_SECTOR_SIZE;
  241. disk->max_agglomerate = GRUB_DISK_MAX_MAX_AGGLOMERATE;
  242. disk->id = 0;
  243. return GRUB_ERR_NONE;
  244. }
  245. static void
  246. grub_cbfsdisk_close (grub_disk_t disk __attribute((unused)))
  247. {
  248. }
  249. static grub_err_t
  250. grub_cbfsdisk_read (grub_disk_t disk __attribute((unused)),
  251. grub_disk_addr_t sector,
  252. grub_size_t size, char *buf)
  253. {
  254. grub_memcpy (buf, cbfsdisk_addr + (sector << GRUB_DISK_SECTOR_BITS),
  255. size << GRUB_DISK_SECTOR_BITS);
  256. return 0;
  257. }
  258. static grub_err_t
  259. grub_cbfsdisk_write (grub_disk_t disk __attribute__ ((unused)),
  260. grub_disk_addr_t sector __attribute__ ((unused)),
  261. grub_size_t size __attribute__ ((unused)),
  262. const char *buf __attribute__ ((unused)))
  263. {
  264. return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
  265. "rom flashing isn't implemented yet");
  266. }
  267. static struct grub_disk_dev grub_cbfsdisk_dev =
  268. {
  269. .name = "cbfsdisk",
  270. .id = GRUB_DISK_DEVICE_CBFSDISK_ID,
  271. .iterate = grub_cbfsdisk_iterate,
  272. .open = grub_cbfsdisk_open,
  273. .close = grub_cbfsdisk_close,
  274. .read = grub_cbfsdisk_read,
  275. .write = grub_cbfsdisk_write,
  276. .next = 0
  277. };
  278. static void
  279. init_cbfsdisk (void)
  280. {
  281. grub_uint32_t ptr;
  282. struct cbfs_header *head;
  283. ptr = *(grub_uint32_t *) 0xfffffffc;
  284. head = (struct cbfs_header *) (grub_addr_t) ptr;
  285. grub_dprintf ("cbfs", "head=%p\n", head);
  286. /* coreboot current supports only ROMs <= 16 MiB. Bigger ROMs will
  287. have problems as RCBA is 18 MiB below end of 32-bit typically,
  288. so either memory map would have to be rearranged or we'd need to support
  289. reading ROMs through controller directly.
  290. */
  291. if (ptr < 0xff000000
  292. || 0xffffffff - ptr < (grub_uint32_t) sizeof (*head) + 0xf
  293. || !validate_head (head))
  294. return;
  295. cbfsdisk_size = ALIGN_UP (grub_be_to_cpu32 (head->romsize),
  296. GRUB_DISK_SECTOR_SIZE);
  297. cbfsdisk_addr = (void *) (grub_addr_t) (0x100000000ULL - cbfsdisk_size);
  298. grub_disk_dev_register (&grub_cbfsdisk_dev);
  299. }
  300. static void
  301. fini_cbfsdisk (void)
  302. {
  303. if (! cbfsdisk_size)
  304. return;
  305. grub_disk_dev_unregister (&grub_cbfsdisk_dev);
  306. }
  307. #endif
  308. static struct grub_fs grub_cbfs_fs = {
  309. .name = "cbfs",
  310. .dir = grub_cbfs_dir,
  311. .open = grub_cbfs_open,
  312. .read = grub_cbfs_read,
  313. .close = grub_cbfs_close,
  314. #ifdef GRUB_UTIL
  315. .reserved_first_sector = 0,
  316. .blocklist_install = 0,
  317. #endif
  318. };
  319. GRUB_MOD_INIT (cbfs)
  320. {
  321. #if (defined (__i386__) || defined (__x86_64__)) && !defined (GRUB_UTIL) && !defined (GRUB_MACHINE_EMU) && !defined (GRUB_MACHINE_XEN)
  322. init_cbfsdisk ();
  323. #endif
  324. grub_fs_register (&grub_cbfs_fs);
  325. }
  326. GRUB_MOD_FINI (cbfs)
  327. {
  328. grub_fs_unregister (&grub_cbfs_fs);
  329. #if (defined (__i386__) || defined (__x86_64__)) && !defined (GRUB_UTIL) && !defined (GRUB_MACHINE_EMU) && !defined (GRUB_MACHINE_XEN)
  330. fini_cbfsdisk ();
  331. #endif
  332. }