offset.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2013 Free Software Foundation, Inc.
  4. *
  5. * GRUB is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * GRUB is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <grub/file.h>
  19. #include <grub/dl.h>
  20. GRUB_MOD_LICENSE ("GPLv3+");
  21. struct grub_offset_file
  22. {
  23. grub_file_t parent;
  24. grub_off_t off;
  25. };
  26. static grub_ssize_t
  27. grub_offset_read (grub_file_t file, char *buf, grub_size_t len)
  28. {
  29. struct grub_offset_file *data = file->data;
  30. if (grub_file_seek (data->parent, data->off + file->offset) == (grub_off_t) -1)
  31. return -1;
  32. return grub_file_read (data->parent, buf, len);
  33. }
  34. static grub_err_t
  35. grub_offset_close (grub_file_t file)
  36. {
  37. struct grub_offset_file *data = file->data;
  38. if (data->parent)
  39. grub_file_close (data->parent);
  40. /* No need to close the same device twice. */
  41. file->device = 0;
  42. return 0;
  43. }
  44. static struct grub_fs grub_offset_fs = {
  45. .name = "offset",
  46. .fs_dir = 0,
  47. .fs_open = 0,
  48. .fs_read = grub_offset_read,
  49. .fs_close = grub_offset_close,
  50. .fs_label = 0,
  51. .next = 0
  52. };
  53. void
  54. grub_file_offset_close (grub_file_t file)
  55. {
  56. struct grub_offset_file *off_data = file->data;
  57. off_data->parent = NULL;
  58. grub_file_close (file);
  59. }
  60. grub_file_t
  61. grub_file_offset_open (grub_file_t parent, enum grub_file_type type,
  62. grub_off_t start, grub_off_t size)
  63. {
  64. struct grub_offset_file *off_data;
  65. grub_file_t off_file, last_off_file;
  66. grub_file_filter_id_t filter;
  67. off_file = grub_zalloc (sizeof (*off_file));
  68. off_data = grub_zalloc (sizeof (*off_data));
  69. if (!off_file || !off_data)
  70. {
  71. grub_free (off_file);
  72. grub_free (off_data);
  73. return 0;
  74. }
  75. off_data->off = start;
  76. off_data->parent = parent;
  77. off_file->device = parent->device;
  78. off_file->data = off_data;
  79. off_file->fs = &grub_offset_fs;
  80. off_file->size = size;
  81. last_off_file = NULL;
  82. for (filter = GRUB_FILE_FILTER_COMPRESSION_FIRST;
  83. off_file && filter <= GRUB_FILE_FILTER_COMPRESSION_LAST; filter++)
  84. if (grub_file_filters[filter])
  85. {
  86. last_off_file = off_file;
  87. off_file = grub_file_filters[filter] (off_file, type);
  88. }
  89. if (!off_file)
  90. {
  91. off_data->parent = NULL;
  92. grub_file_close (last_off_file);
  93. return 0;
  94. }
  95. return off_file;
  96. }