[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PROCFS 5/5] procfs: Add (proc)/devices to get info on grub devices.
From: |
Glenn Washburn |
Subject: |
[PROCFS 5/5] procfs: Add (proc)/devices to get info on grub devices. |
Date: |
Fri, 19 Mar 2021 18:17:46 -0500 |
Signed-off-by: Glenn Washburn <development@efficientek.com>
---
grub-core/fs/proc.c | 91 +++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 91 insertions(+)
diff --git a/grub-core/fs/proc.c b/grub-core/fs/proc.c
index 5f516502d..9baabb7d4 100644
--- a/grub-core/fs/proc.c
+++ b/grub-core/fs/proc.c
@@ -190,10 +190,101 @@ static struct grub_fs grub_procfs_fs =
.next = 0
};
+/* Context for grub_device_iterate. */
+struct grub_device_iterate_ctx
+{
+ char *str;
+ grub_size_t len, size;
+};
+
+#define ALLOC_SIZE 512
+static int
+iterate_disk (const char *disk_name, void *data)
+{
+ struct grub_device_iterate_ctx *ctx = data;
+ grub_device_t dev;
+ int size = 0, ret = 0;
+ char *buf;
+
+ dev = grub_device_open (disk_name);
+ if (! dev)
+ {
+ grub_errno = GRUB_ERR_NONE;
+ return 0;
+ }
+
+ if (!dev->disk || dev->disk->partition)
+ goto err;
+
+ do {
+ grub_disk_t disk = dev->disk;
+
+ size = grub_snprintf(ctx->str + ctx->len, ctx->size - ctx->len,
+ "\n%s %s %"PRIuGRUB_UINT64_T" %u",
+ disk->name, (disk->dev) ? disk->dev->name : "none",
+ disk->total_sectors, 1 << disk->log_sector_size);
+
+ if ((size + ctx->len) <= ctx->size)
+ {
+ ctx->len += size;
+ break;
+ }
+
+ /* Not enough space left in string, reallocate more and try again. */
+ buf = grub_realloc(ctx->str, ctx->size + ALLOC_SIZE);
+ if (!buf)
+ goto err;
+
+ ctx->str = buf;
+ ctx->size += ALLOC_SIZE;
+ } while (1);
+
+err:
+ grub_device_close (dev);
+ return ret;
+}
+
+static char *
+devices_get (grub_size_t *sz)
+{
+ struct grub_device_iterate_ctx ctx = { NULL, 0, 0 };
+ static const char header[] = N_("<name> <type> <nsectors> <sector size>");
+
+ *sz = 0;
+ ctx.str = grub_malloc(ALLOC_SIZE);
+ ctx.size = ALLOC_SIZE;
+ if (ctx.str == NULL)
+ {
+ grub_print_error();
+ return NULL;
+ }
+
+ ctx.len = grub_stpcpy (ctx.str, header) - ctx.str;
+
+ if (grub_disk_dev_iterate (iterate_disk, &ctx))
+ grub_print_error();
+
+ *sz = ctx.len;
+ return ctx.str;
+}
+
+struct grub_procfs_entry procfs[] =
+{
+ {
+ .name = "devices",
+ .get_contents = devices_get
+ }
+};
+
GRUB_MOD_INIT (procfs)
{
+ struct grub_procfs_entry *pe;
+
grub_disk_dev_register (&grub_procfs_dev);
grub_fs_register (&grub_procfs_fs);
+
+ for (pe = procfs + (sizeof (procfs)/sizeof (*procfs)) - 1; (pe + 1) !=
procfs; pe--)
+ grub_procfs_register (pe->name, pe);
}
GRUB_MOD_FINI (procfs)
--
2.27.0