cpio_common.c 5.5 KB

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