cpio_common.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /* cpio.c - cpio 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/file.h>
  20. #include <grub/mm.h>
  21. #include <grub/misc.h>
  22. #include <grub/disk.h>
  23. #include <grub/dl.h>
  24. #include <grub/i18n.h>
  25. #include <grub/archelp.h>
  26. GRUB_MOD_LICENSE ("GPLv3+");
  27. struct grub_archelp_data
  28. {
  29. grub_disk_t disk;
  30. grub_off_t hofs;
  31. grub_off_t next_hofs;
  32. grub_off_t dofs;
  33. grub_off_t size;
  34. };
  35. #if __GNUC__ >= 9
  36. #pragma GCC diagnostic push
  37. #pragma GCC diagnostic ignored "-Waddress-of-packed-member"
  38. #endif
  39. static grub_err_t
  40. grub_cpio_find_file (struct grub_archelp_data *data, char **name,
  41. grub_int32_t *mtime, grub_uint32_t *mode)
  42. {
  43. struct head hd;
  44. grub_size_t namesize;
  45. grub_uint32_t modeval;
  46. data->hofs = data->next_hofs;
  47. if (grub_disk_read (data->disk, 0, data->hofs, sizeof (hd), &hd))
  48. return grub_errno;
  49. if (grub_memcmp (hd.magic, MAGIC, sizeof (hd.magic)) != 0
  50. #ifdef MAGIC2
  51. && grub_memcmp (hd.magic, MAGIC2, sizeof (hd.magic)) != 0
  52. #endif
  53. )
  54. return grub_error (GRUB_ERR_BAD_FS, "invalid cpio archive");
  55. data->size = read_number (hd.filesize, ARRAY_SIZE (hd.filesize));
  56. if (mtime)
  57. *mtime = read_number (hd.mtime, ARRAY_SIZE (hd.mtime));
  58. modeval = read_number (hd.mode, ARRAY_SIZE (hd.mode));
  59. namesize = read_number (hd.namesize, ARRAY_SIZE (hd.namesize));
  60. /* Don't allow negative numbers. */
  61. if (namesize >= 0x80000000)
  62. {
  63. /* Probably a corruption, don't attempt to recover. */
  64. *mode = GRUB_ARCHELP_ATTR_END;
  65. return GRUB_ERR_NONE;
  66. }
  67. *mode = modeval;
  68. *name = grub_malloc (namesize + 1);
  69. if (*name == NULL)
  70. return grub_errno;
  71. if (grub_disk_read (data->disk, 0, data->hofs + sizeof (hd),
  72. namesize, *name))
  73. {
  74. grub_free (*name);
  75. return grub_errno;
  76. }
  77. (*name)[namesize] = 0;
  78. if (data->size == 0 && modeval == 0 && namesize == 11
  79. && grub_memcmp(*name, "TRAILER!!!", 11) == 0)
  80. {
  81. *mode = GRUB_ARCHELP_ATTR_END;
  82. grub_free (*name);
  83. return GRUB_ERR_NONE;
  84. }
  85. data->dofs = data->hofs + ALIGN_CPIO (sizeof (hd) + namesize);
  86. data->next_hofs = data->dofs + ALIGN_CPIO (data->size);
  87. return GRUB_ERR_NONE;
  88. }
  89. #if __GNUC__ >= 9
  90. #pragma GCC diagnostic pop
  91. #endif
  92. static char *
  93. grub_cpio_get_link_target (struct grub_archelp_data *data)
  94. {
  95. char *ret;
  96. grub_err_t err;
  97. if (data->size == 0)
  98. return grub_strdup ("");
  99. ret = grub_malloc (data->size + 1);
  100. if (!ret)
  101. return NULL;
  102. err = grub_disk_read (data->disk, 0, data->dofs, data->size,
  103. ret);
  104. if (err)
  105. {
  106. grub_free (ret);
  107. return NULL;
  108. }
  109. ret[data->size] = '\0';
  110. return ret;
  111. }
  112. static void
  113. grub_cpio_rewind (struct grub_archelp_data *data)
  114. {
  115. data->next_hofs = 0;
  116. }
  117. static struct grub_archelp_ops arcops =
  118. {
  119. .find_file = grub_cpio_find_file,
  120. .get_link_target = grub_cpio_get_link_target,
  121. .rewind = grub_cpio_rewind
  122. };
  123. static struct grub_archelp_data *
  124. grub_cpio_mount (grub_disk_t disk)
  125. {
  126. struct head hd;
  127. struct grub_archelp_data *data;
  128. if (grub_disk_read (disk, 0, 0, sizeof (hd), &hd))
  129. goto fail;
  130. if (grub_memcmp (hd.magic, MAGIC, sizeof (MAGIC) - 1)
  131. #ifdef MAGIC2
  132. && grub_memcmp (hd.magic, MAGIC2, sizeof (MAGIC2) - 1)
  133. #endif
  134. )
  135. goto fail;
  136. data = (struct grub_archelp_data *) grub_zalloc (sizeof (*data));
  137. if (!data)
  138. goto fail;
  139. data->disk = disk;
  140. return data;
  141. fail:
  142. grub_error (GRUB_ERR_BAD_FS, "not a " FSNAME " filesystem");
  143. return 0;
  144. }
  145. static grub_err_t
  146. grub_cpio_dir (grub_device_t device, const char *path_in,
  147. grub_fs_dir_hook_t hook, void *hook_data)
  148. {
  149. struct grub_archelp_data *data;
  150. grub_err_t err;
  151. data = grub_cpio_mount (device->disk);
  152. if (!data)
  153. return grub_errno;
  154. err = grub_archelp_dir (data, &arcops,
  155. path_in, hook, hook_data);
  156. grub_free (data);
  157. return err;
  158. }
  159. static grub_err_t
  160. grub_cpio_open (grub_file_t file, const char *name_in)
  161. {
  162. struct grub_archelp_data *data;
  163. grub_err_t err;
  164. data = grub_cpio_mount (file->device->disk);
  165. if (!data)
  166. return grub_errno;
  167. err = grub_archelp_open (data, &arcops, name_in);
  168. if (err)
  169. {
  170. grub_free (data);
  171. }
  172. else
  173. {
  174. file->data = data;
  175. file->size = data->size;
  176. }
  177. return err;
  178. }
  179. static grub_ssize_t
  180. grub_cpio_read (grub_file_t file, char *buf, grub_size_t len)
  181. {
  182. struct grub_archelp_data *data;
  183. grub_ssize_t ret;
  184. data = file->data;
  185. data->disk->read_hook = file->read_hook;
  186. data->disk->read_hook_data = file->read_hook_data;
  187. ret = (grub_disk_read (data->disk, 0, data->dofs + file->offset,
  188. len, buf)) ? -1 : (grub_ssize_t) len;
  189. data->disk->read_hook = 0;
  190. return ret;
  191. }
  192. static grub_err_t
  193. grub_cpio_close (grub_file_t file)
  194. {
  195. struct grub_archelp_data *data;
  196. data = file->data;
  197. grub_free (data);
  198. return grub_errno;
  199. }
  200. static struct grub_fs grub_cpio_fs = {
  201. .name = FSNAME,
  202. .fs_dir = grub_cpio_dir,
  203. .fs_open = grub_cpio_open,
  204. .fs_read = grub_cpio_read,
  205. .fs_close = grub_cpio_close,
  206. #ifdef GRUB_UTIL
  207. .reserved_first_sector = 0,
  208. .blocklist_install = 0,
  209. #endif
  210. };