proc.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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/procfs.h>
  19. #include <grub/disk.h>
  20. #include <grub/fs.h>
  21. #include <grub/file.h>
  22. #include <grub/mm.h>
  23. #include <grub/dl.h>
  24. #include <grub/archelp.h>
  25. GRUB_MOD_LICENSE ("GPLv3+");
  26. struct grub_procfs_entry *grub_procfs_entries;
  27. static int
  28. grub_procdev_iterate (grub_disk_dev_iterate_hook_t hook, void *hook_data,
  29. grub_disk_pull_t pull)
  30. {
  31. if (pull != GRUB_DISK_PULL_NONE)
  32. return 0;
  33. return hook ("proc", hook_data);
  34. }
  35. static grub_err_t
  36. grub_procdev_open (const char *name, grub_disk_t disk)
  37. {
  38. if (grub_strcmp (name, "proc"))
  39. return grub_error (GRUB_ERR_UNKNOWN_DEVICE, "not a procfs disk");
  40. disk->total_sectors = 0;
  41. disk->id = 0;
  42. disk->data = 0;
  43. return GRUB_ERR_NONE;
  44. }
  45. static void
  46. grub_procdev_close (grub_disk_t disk __attribute((unused)))
  47. {
  48. }
  49. static grub_err_t
  50. grub_procdev_read (grub_disk_t disk __attribute((unused)),
  51. grub_disk_addr_t sector __attribute((unused)),
  52. grub_size_t size __attribute((unused)),
  53. char *buf __attribute((unused)))
  54. {
  55. return GRUB_ERR_OUT_OF_RANGE;
  56. }
  57. static grub_err_t
  58. grub_procdev_write (grub_disk_t disk __attribute ((unused)),
  59. grub_disk_addr_t sector __attribute ((unused)),
  60. grub_size_t size __attribute ((unused)),
  61. const char *buf __attribute ((unused)))
  62. {
  63. return GRUB_ERR_OUT_OF_RANGE;
  64. }
  65. struct grub_archelp_data
  66. {
  67. struct grub_procfs_entry *entry, *next_entry;
  68. };
  69. static void
  70. grub_procfs_rewind (struct grub_archelp_data *data)
  71. {
  72. data->entry = NULL;
  73. data->next_entry = grub_procfs_entries;
  74. }
  75. static grub_err_t
  76. grub_procfs_find_file (struct grub_archelp_data *data, char **name,
  77. grub_int32_t *mtime,
  78. grub_uint32_t *mode)
  79. {
  80. data->entry = data->next_entry;
  81. if (!data->entry)
  82. {
  83. *mode = GRUB_ARCHELP_ATTR_END;
  84. return GRUB_ERR_NONE;
  85. }
  86. data->next_entry = data->entry->next;
  87. *mode = GRUB_ARCHELP_ATTR_FILE | GRUB_ARCHELP_ATTR_NOTIME;
  88. *name = grub_strdup (data->entry->name);
  89. *mtime = 0;
  90. if (!*name)
  91. return grub_errno;
  92. return GRUB_ERR_NONE;
  93. }
  94. static struct grub_archelp_ops arcops =
  95. {
  96. .find_file = grub_procfs_find_file,
  97. .rewind = grub_procfs_rewind
  98. };
  99. static grub_ssize_t
  100. grub_procfs_read (grub_file_t file, char *buf, grub_size_t len)
  101. {
  102. char *data = file->data;
  103. grub_memcpy (buf, data + file->offset, len);
  104. return len;
  105. }
  106. static grub_err_t
  107. grub_procfs_close (grub_file_t file)
  108. {
  109. char *data;
  110. data = file->data;
  111. grub_free (data);
  112. return GRUB_ERR_NONE;
  113. }
  114. static grub_err_t
  115. grub_procfs_dir (grub_device_t device, const char *path,
  116. grub_fs_dir_hook_t hook, void *hook_data)
  117. {
  118. struct grub_archelp_data data;
  119. /* Check if the disk is our dummy disk. */
  120. if (grub_strcmp (device->disk->name, "proc"))
  121. return grub_error (GRUB_ERR_BAD_FS, "not a procfs");
  122. grub_procfs_rewind (&data);
  123. return grub_archelp_dir (&data, &arcops,
  124. path, hook, hook_data);
  125. }
  126. static grub_err_t
  127. grub_procfs_open (struct grub_file *file, const char *path)
  128. {
  129. grub_err_t err;
  130. struct grub_archelp_data data;
  131. grub_size_t sz;
  132. grub_procfs_rewind (&data);
  133. err = grub_archelp_open (&data, &arcops, path);
  134. if (err)
  135. return err;
  136. file->data = data.entry->get_contents (&sz);
  137. if (!file->data)
  138. return grub_errno;
  139. file->size = sz;
  140. return GRUB_ERR_NONE;
  141. }
  142. static struct grub_disk_dev grub_procfs_dev = {
  143. .name = "proc",
  144. .id = GRUB_DISK_DEVICE_PROCFS_ID,
  145. .disk_iterate = grub_procdev_iterate,
  146. .disk_open = grub_procdev_open,
  147. .disk_close = grub_procdev_close,
  148. .disk_read = grub_procdev_read,
  149. .disk_write = grub_procdev_write,
  150. .next = 0
  151. };
  152. static struct grub_fs grub_procfs_fs =
  153. {
  154. .name = "procfs",
  155. .fs_dir = grub_procfs_dir,
  156. .fs_open = grub_procfs_open,
  157. .fs_read = grub_procfs_read,
  158. .fs_close = grub_procfs_close,
  159. .next = 0
  160. };
  161. GRUB_MOD_INIT (procfs)
  162. {
  163. grub_disk_dev_register (&grub_procfs_dev);
  164. grub_fs_register (&grub_procfs_fs);
  165. }
  166. GRUB_MOD_FINI (procfs)
  167. {
  168. grub_disk_dev_unregister (&grub_procfs_dev);
  169. grub_fs_unregister (&grub_procfs_fs);
  170. }