summaryrefslogtreecommitdiffstats
path: root/grub-core/fs/proc.c
blob: 5f516502d41969628c6a9514255b180464c461ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*
 *  GRUB  --  GRand Unified Bootloader
 *  Copyright (C) 2013  Free Software Foundation, Inc.
 *
 *  GRUB is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  GRUB is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <grub/procfs.h>
#include <grub/disk.h>
#include <grub/fs.h>
#include <grub/file.h>
#include <grub/mm.h>
#include <grub/dl.h>
#include <grub/archelp.h>

GRUB_MOD_LICENSE ("GPLv3+");

struct grub_procfs_entry *grub_procfs_entries;

static int
grub_procdev_iterate (grub_disk_dev_iterate_hook_t hook, void *hook_data,
			 grub_disk_pull_t pull)
{
  if (pull != GRUB_DISK_PULL_NONE)
    return 0;

  return hook ("proc", hook_data);
}

static grub_err_t
grub_procdev_open (const char *name, grub_disk_t disk)
{
  if (grub_strcmp (name, "proc"))
      return grub_error (GRUB_ERR_UNKNOWN_DEVICE, "not a procfs disk");

  disk->total_sectors = 0;
  disk->id = 0;

  disk->data = 0;

  return GRUB_ERR_NONE;
}

static void
grub_procdev_close (grub_disk_t disk __attribute((unused)))
{
}

static grub_err_t
grub_procdev_read (grub_disk_t disk __attribute((unused)),
		grub_disk_addr_t sector __attribute((unused)),
		grub_size_t size __attribute((unused)),
		char *buf __attribute((unused)))
{
  return GRUB_ERR_OUT_OF_RANGE;
}

static grub_err_t
grub_procdev_write (grub_disk_t disk __attribute ((unused)),
		       grub_disk_addr_t sector __attribute ((unused)),
		       grub_size_t size __attribute ((unused)),
		       const char *buf __attribute ((unused)))
{
  return GRUB_ERR_OUT_OF_RANGE;
}

struct grub_archelp_data
{
  struct grub_procfs_entry *entry, *next_entry;
};

static void
grub_procfs_rewind (struct grub_archelp_data *data)
{
  data->entry = NULL;
  data->next_entry = grub_procfs_entries;
}

static grub_err_t
grub_procfs_find_file (struct grub_archelp_data *data, char **name,
		     grub_int32_t *mtime,
		     grub_uint32_t *mode)
{
  data->entry = data->next_entry;
  if (!data->entry)
    {
      *mode = GRUB_ARCHELP_ATTR_END;
      return GRUB_ERR_NONE;
    }
  data->next_entry = data->entry->next;
  *mode = GRUB_ARCHELP_ATTR_FILE | GRUB_ARCHELP_ATTR_NOTIME;
  *name = grub_strdup (data->entry->name);
  *mtime = 0;
  if (!*name)
    return grub_errno;
  return GRUB_ERR_NONE;
}

static struct grub_archelp_ops arcops =
  {
    .find_file = grub_procfs_find_file,
    .rewind = grub_procfs_rewind
  };

static grub_ssize_t
grub_procfs_read (grub_file_t file, char *buf, grub_size_t len)
{
  char *data = file->data;

  grub_memcpy (buf, data + file->offset, len);

  return len;
}

static grub_err_t
grub_procfs_close (grub_file_t file)
{
  char *data;

  data = file->data;
  grub_free (data);

  return GRUB_ERR_NONE;
}

static grub_err_t
grub_procfs_dir (grub_device_t device, const char *path,
		 grub_fs_dir_hook_t hook, void *hook_data)
{
  struct grub_archelp_data data;

  /* Check if the disk is our dummy disk.  */
  if (grub_strcmp (device->disk->name, "proc"))
    return grub_error (GRUB_ERR_BAD_FS, "not a procfs");

  grub_procfs_rewind (&data);

  return grub_archelp_dir (&data, &arcops,
			   path, hook, hook_data);
}

static grub_err_t
grub_procfs_open (struct grub_file *file, const char *path)
{
  grub_err_t err;
  struct grub_archelp_data data;
  grub_size_t sz;

  grub_procfs_rewind (&data);

  err = grub_archelp_open (&data, &arcops, path);
  if (err)
    return err;
  file->data = data.entry->get_contents (&sz);
  if (!file->data)
    return grub_errno;
  file->size = sz;
  return GRUB_ERR_NONE;
}

static struct grub_disk_dev grub_procfs_dev = {
  .name = "proc",
  .id = GRUB_DISK_DEVICE_PROCFS_ID,
  .disk_iterate = grub_procdev_iterate,
  .disk_open = grub_procdev_open,
  .disk_close = grub_procdev_close,
  .disk_read = grub_procdev_read,
  .disk_write = grub_procdev_write,
  .next = 0
};

static struct grub_fs grub_procfs_fs =
  {
    .name = "procfs",
    .fs_dir = grub_procfs_dir,
    .fs_open = grub_procfs_open,
    .fs_read = grub_procfs_read,
    .fs_close = grub_procfs_close,
    .next = 0
  };

GRUB_MOD_INIT (procfs)
{
  grub_disk_dev_register (&grub_procfs_dev);
  grub_fs_register (&grub_procfs_fs);
}

GRUB_MOD_FINI (procfs)
{
  grub_disk_dev_unregister (&grub_procfs_dev);
  grub_fs_unregister (&grub_procfs_fs);
}