2 * GRUB -- GRand Unified Bootloader
3 * Copyright (C) 2013 Free Software Foundation, Inc.
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.
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.
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/>.
19 #include <grub/procfs.h>
20 #include <grub/disk.h>
22 #include <grub/file.h>
26 GRUB_MOD_LICENSE ("GPLv3+");
28 struct grub_procfs_entry
*grub_procfs_entries
;
31 grub_procdev_iterate (grub_disk_dev_iterate_hook_t hook
, void *hook_data
,
32 grub_disk_pull_t pull
)
34 if (pull
!= GRUB_DISK_PULL_NONE
)
37 return hook ("proc", hook_data
);
41 grub_procdev_open (const char *name
, grub_disk_t disk
)
43 if (grub_strcmp (name
, "proc"))
44 return grub_error (GRUB_ERR_UNKNOWN_DEVICE
, "not a procfs disk");
46 disk
->total_sectors
= 0;
55 grub_procdev_close (grub_disk_t disk
__attribute((unused
)))
60 grub_procdev_read (grub_disk_t disk
__attribute((unused
)),
61 grub_disk_addr_t sector
__attribute((unused
)),
62 grub_size_t size
__attribute((unused
)),
63 char *buf
__attribute((unused
)))
65 return GRUB_ERR_OUT_OF_RANGE
;
69 grub_procdev_write (grub_disk_t disk
__attribute ((unused
)),
70 grub_disk_addr_t sector
__attribute ((unused
)),
71 grub_size_t size
__attribute ((unused
)),
72 const char *buf
__attribute ((unused
)))
74 return GRUB_ERR_OUT_OF_RANGE
;
78 grub_procfs_read (grub_file_t file
, char *buf
, grub_size_t len
)
80 char *data
= file
->data
;
82 grub_memcpy (buf
, data
+ file
->offset
, len
);
88 grub_procfs_close (grub_file_t file
)
99 grub_procfs_dir (grub_device_t device
, const char *path
,
100 grub_fs_dir_hook_t hook
, void *hook_data
)
103 struct grub_dirhook_info info
;
104 struct grub_procfs_entry
*entry
;
106 grub_memset (&info
, 0, sizeof (info
));
108 /* Check if the disk is our dummy disk. */
109 if (grub_strcmp (device
->disk
->name
, "proc"))
110 return grub_error (GRUB_ERR_BAD_FS
, "not a procfs");
111 for (ptr
= path
; *ptr
== '/'; ptr
++);
114 FOR_LIST_ELEMENTS((entry
), (grub_procfs_entries
))
115 if (hook (entry
->name
, &info
, hook_data
))
121 grub_procfs_open (struct grub_file
*file
, const char *path
)
124 struct grub_procfs_entry
*entry
;
126 for (pathptr
= path
; *pathptr
== '/'; pathptr
++);
128 FOR_LIST_ELEMENTS((entry
), (grub_procfs_entries
))
129 if (grub_strcmp (pathptr
, entry
->name
) == 0)
132 file
->data
= entry
->get_contents (&sz
);
136 return GRUB_ERR_NONE
;
139 return grub_error (GRUB_ERR_FILE_NOT_FOUND
, N_("file `%s' not found"), path
);
142 static struct grub_disk_dev grub_procfs_dev
= {
144 .id
= GRUB_DISK_DEVICE_PROCFS_ID
,
145 .iterate
= grub_procdev_iterate
,
146 .open
= grub_procdev_open
,
147 .close
= grub_procdev_close
,
148 .read
= grub_procdev_read
,
149 .write
= grub_procdev_write
,
153 static struct grub_fs grub_procfs_fs
=
156 .dir
= grub_procfs_dir
,
157 .open
= grub_procfs_open
,
158 .read
= grub_procfs_read
,
159 .close
= grub_procfs_close
,
163 GRUB_MOD_INIT (procfs
)
165 grub_disk_dev_register (&grub_procfs_dev
);
166 grub_fs_register (&grub_procfs_fs
);
169 GRUB_MOD_FINI (procfs
)
171 grub_disk_dev_unregister (&grub_procfs_dev
);
172 grub_fs_unregister (&grub_procfs_fs
);