dcavalca / rpms / grub2

Forked from rpms/grub2 3 years ago
Clone
b1bcb2
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
b1bcb2
From: Chris Coulson <chris.coulson@canonical.com>
b1bcb2
Date: Thu, 7 Jan 2021 15:15:43 +0000
b1bcb2
Subject: [PATCH] kern/buffer: Add variable sized heap buffer
b1bcb2
b1bcb2
Add a new variable sized heap buffer type (grub_buffer_t) with simple
b1bcb2
operations for appending data, accessing the data and maintaining
b1bcb2
a read cursor.
b1bcb2
b1bcb2
Signed-off-by: Chris Coulson <chris.coulson@canonical.com>
b1bcb2
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
b1bcb2
---
b1bcb2
 grub-core/Makefile.core.def |   1 +
b1bcb2
 grub-core/kern/buffer.c     | 117 +++++++++++++++++++++++++++++++++++
b1bcb2
 include/grub/buffer.h       | 144 ++++++++++++++++++++++++++++++++++++++++++++
b1bcb2
 3 files changed, 262 insertions(+)
b1bcb2
 create mode 100644 grub-core/kern/buffer.c
b1bcb2
 create mode 100644 include/grub/buffer.h
b1bcb2
b1bcb2
diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def
b1bcb2
index 2beb1d83a63..5228d3d530d 100644
b1bcb2
--- a/grub-core/Makefile.core.def
b1bcb2
+++ b/grub-core/Makefile.core.def
b1bcb2
@@ -114,6 +114,7 @@ kernel = {
b1bcb2
   arm_efi_startup = kern/arm/efi/startup.S;
b1bcb2
   arm64_efi_startup = kern/arm64/efi/startup.S;
b1bcb2
 
b1bcb2
+  common = kern/buffer.c;
b1bcb2
   common = kern/command.c;
b1bcb2
   common = kern/corecmd.c;
b1bcb2
   common = kern/device.c;
b1bcb2
diff --git a/grub-core/kern/buffer.c b/grub-core/kern/buffer.c
b1bcb2
new file mode 100644
b1bcb2
index 00000000000..9f5f8b86705
b1bcb2
--- /dev/null
b1bcb2
+++ b/grub-core/kern/buffer.c
b1bcb2
@@ -0,0 +1,117 @@
b1bcb2
+/*
b1bcb2
+ *  GRUB  --  GRand Unified Bootloader
b1bcb2
+ *  Copyright (C) 2021  Free Software Foundation, Inc.
b1bcb2
+ *
b1bcb2
+ *  GRUB is free software: you can redistribute it and/or modify
b1bcb2
+ *  it under the terms of the GNU General Public License as published by
b1bcb2
+ *  the Free Software Foundation, either version 3 of the License, or
b1bcb2
+ *  (at your option) any later version.
b1bcb2
+ *
b1bcb2
+ *  GRUB is distributed in the hope that it will be useful,
b1bcb2
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
b1bcb2
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
b1bcb2
+ *  GNU General Public License for more details.
b1bcb2
+ *
b1bcb2
+ *  You should have received a copy of the GNU General Public License
b1bcb2
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
b1bcb2
+ */
b1bcb2
+
b1bcb2
+#include <grub/buffer.h>
b1bcb2
+#include <grub/err.h>
b1bcb2
+#include <grub/misc.h>
b1bcb2
+#include <grub/mm.h>
b1bcb2
+#include <grub/safemath.h>
b1bcb2
+#include <grub/types.h>
b1bcb2
+
b1bcb2
+grub_buffer_t
b1bcb2
+grub_buffer_new (grub_size_t sz)
b1bcb2
+{
b1bcb2
+  struct grub_buffer *ret;
b1bcb2
+
b1bcb2
+  ret = (struct grub_buffer *) grub_malloc (sizeof (*ret));
b1bcb2
+  if (ret == NULL)
b1bcb2
+    return NULL;
b1bcb2
+
b1bcb2
+  ret->data = (grub_uint8_t *) grub_malloc (sz);
b1bcb2
+  if (ret->data == NULL)
b1bcb2
+    {
b1bcb2
+      grub_free (ret);
b1bcb2
+      return NULL;
b1bcb2
+    }
b1bcb2
+
b1bcb2
+  ret->sz = sz;
b1bcb2
+  ret->pos = 0;
b1bcb2
+  ret->used = 0;
b1bcb2
+
b1bcb2
+  return ret;
b1bcb2
+}
b1bcb2
+
b1bcb2
+void
b1bcb2
+grub_buffer_free (grub_buffer_t buf)
b1bcb2
+{
b1bcb2
+  grub_free (buf->data);
b1bcb2
+  grub_free (buf);
b1bcb2
+}
b1bcb2
+
b1bcb2
+grub_err_t
b1bcb2
+grub_buffer_ensure_space (grub_buffer_t buf, grub_size_t req)
b1bcb2
+{
b1bcb2
+  grub_uint8_t *d;
b1bcb2
+  grub_size_t newsz = 1;
b1bcb2
+
b1bcb2
+  /* Is the current buffer size adequate? */
b1bcb2
+  if (buf->sz >= req)
b1bcb2
+    return GRUB_ERR_NONE;
b1bcb2
+
b1bcb2
+  /* Find the smallest power-of-2 size that satisfies the request. */
b1bcb2
+  while (newsz < req)
b1bcb2
+    {
b1bcb2
+      if (newsz == 0)
b1bcb2
+	return grub_error (GRUB_ERR_OUT_OF_RANGE,
b1bcb2
+			   N_("requested buffer size is too large"));
b1bcb2
+      newsz <<= 1;
b1bcb2
+    }
b1bcb2
+
b1bcb2
+  d = (grub_uint8_t *) grub_realloc (buf->data, newsz);
b1bcb2
+  if (d == NULL)
b1bcb2
+    return grub_errno;
b1bcb2
+
b1bcb2
+  buf->data = d;
b1bcb2
+  buf->sz = newsz;
b1bcb2
+
b1bcb2
+  return GRUB_ERR_NONE;
b1bcb2
+}
b1bcb2
+
b1bcb2
+void *
b1bcb2
+grub_buffer_take_data (grub_buffer_t buf)
b1bcb2
+{
b1bcb2
+  void *data = buf->data;
b1bcb2
+
b1bcb2
+  buf->data = NULL;
b1bcb2
+  buf->sz = buf->pos = buf->used = 0;
b1bcb2
+
b1bcb2
+  return data;
b1bcb2
+}
b1bcb2
+
b1bcb2
+void
b1bcb2
+grub_buffer_reset (grub_buffer_t buf)
b1bcb2
+{
b1bcb2
+  buf->pos = buf->used = 0;
b1bcb2
+}
b1bcb2
+
b1bcb2
+grub_err_t
b1bcb2
+grub_buffer_advance_read_pos (grub_buffer_t buf, grub_size_t n)
b1bcb2
+{
b1bcb2
+  grub_size_t newpos;
b1bcb2
+
b1bcb2
+  if (grub_add (buf->pos, n, &newpos))
b1bcb2
+    return grub_error (GRUB_ERR_OUT_OF_RANGE, N_("overflow is detected"));
b1bcb2
+
b1bcb2
+  if (newpos > buf->used)
b1bcb2
+    return grub_error (GRUB_ERR_OUT_OF_RANGE,
b1bcb2
+		       N_("new read is position beyond the end of the written data"));
b1bcb2
+
b1bcb2
+  buf->pos = newpos;
b1bcb2
+
b1bcb2
+  return GRUB_ERR_NONE;
b1bcb2
+}
b1bcb2
diff --git a/include/grub/buffer.h b/include/grub/buffer.h
b1bcb2
new file mode 100644
b1bcb2
index 00000000000..f4b10cf2810
b1bcb2
--- /dev/null
b1bcb2
+++ b/include/grub/buffer.h
b1bcb2
@@ -0,0 +1,144 @@
b1bcb2
+/*
b1bcb2
+ *  GRUB  --  GRand Unified Bootloader
b1bcb2
+ *  Copyright (C) 2021  Free Software Foundation, Inc.
b1bcb2
+ *
b1bcb2
+ *  GRUB is free software: you can redistribute it and/or modify
b1bcb2
+ *  it under the terms of the GNU General Public License as published by
b1bcb2
+ *  the Free Software Foundation, either version 3 of the License, or
b1bcb2
+ *  (at your option) any later version.
b1bcb2
+ *
b1bcb2
+ *  GRUB is distributed in the hope that it will be useful,
b1bcb2
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
b1bcb2
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
b1bcb2
+ *  GNU General Public License for more details.
b1bcb2
+ *
b1bcb2
+ *  You should have received a copy of the GNU General Public License
b1bcb2
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
b1bcb2
+ */
b1bcb2
+
b1bcb2
+#ifndef GRUB_BUFFER_H
b1bcb2
+#define GRUB_BUFFER_H	1
b1bcb2
+
b1bcb2
+#include <grub/err.h>
b1bcb2
+#include <grub/misc.h>
b1bcb2
+#include <grub/mm.h>
b1bcb2
+#include <grub/safemath.h>
b1bcb2
+#include <grub/types.h>
b1bcb2
+
b1bcb2
+struct grub_buffer
b1bcb2
+{
b1bcb2
+  grub_uint8_t *data;
b1bcb2
+  grub_size_t sz;
b1bcb2
+  grub_size_t pos;
b1bcb2
+  grub_size_t used;
b1bcb2
+};
b1bcb2
+
b1bcb2
+/*
b1bcb2
+ * grub_buffer_t represents a simple variable sized byte buffer with
b1bcb2
+ * read and write cursors. It currently only implements
b1bcb2
+ * functionality required by the only user in GRUB (append byte[s],
b1bcb2
+ * peeking data at a specified position and updating the read cursor.
b1bcb2
+ * Some things that this doesn't do yet are:
b1bcb2
+ * - Reading a portion of the buffer by copying data from the current
b1bcb2
+ *   read position in to a caller supplied destination buffer and then
b1bcb2
+ *   automatically updating the read cursor.
b1bcb2
+ * - Dropping the read part at the start of the buffer when an append
b1bcb2
+ *   requires more space.
b1bcb2
+ */
b1bcb2
+typedef struct grub_buffer *grub_buffer_t;
b1bcb2
+
b1bcb2
+/* Allocate a new buffer with the specified initial size. */
b1bcb2
+extern grub_buffer_t grub_buffer_new (grub_size_t sz);
b1bcb2
+
b1bcb2
+/* Free the buffer and its resources. */
b1bcb2
+extern void grub_buffer_free (grub_buffer_t buf);
b1bcb2
+
b1bcb2
+/* Return the number of unread bytes in this buffer. */
b1bcb2
+static inline grub_size_t
b1bcb2
+grub_buffer_get_unread_bytes (grub_buffer_t buf)
b1bcb2
+{
b1bcb2
+  return buf->used - buf->pos;
b1bcb2
+}
b1bcb2
+
b1bcb2
+/*
b1bcb2
+ * Ensure that the buffer size is at least the requested
b1bcb2
+ * number of bytes.
b1bcb2
+ */
b1bcb2
+extern grub_err_t grub_buffer_ensure_space (grub_buffer_t buf, grub_size_t req);
b1bcb2
+
b1bcb2
+/*
b1bcb2
+ * Append the specified number of bytes from the supplied
b1bcb2
+ * data to the buffer.
b1bcb2
+ */
b1bcb2
+static inline grub_err_t
b1bcb2
+grub_buffer_append_data (grub_buffer_t buf, const void *data, grub_size_t len)
b1bcb2
+{
b1bcb2
+  grub_size_t req;
b1bcb2
+
b1bcb2
+  if (grub_add (buf->used, len, &req))
b1bcb2
+    return grub_error (GRUB_ERR_OUT_OF_RANGE, N_("overflow is detected"));
b1bcb2
+
b1bcb2
+  if (grub_buffer_ensure_space (buf, req) != GRUB_ERR_NONE)
b1bcb2
+    return grub_errno;
b1bcb2
+
b1bcb2
+  grub_memcpy (&buf->data[buf->used], data, len);
b1bcb2
+  buf->used = req;
b1bcb2
+
b1bcb2
+  return GRUB_ERR_NONE;
b1bcb2
+}
b1bcb2
+
b1bcb2
+/* Append the supplied character to the buffer. */
b1bcb2
+static inline grub_err_t
b1bcb2
+grub_buffer_append_char (grub_buffer_t buf, char c)
b1bcb2
+{
b1bcb2
+  return grub_buffer_append_data (buf, &c, 1);
b1bcb2
+}
b1bcb2
+
b1bcb2
+/*
b1bcb2
+ * Forget and return the underlying data buffer. The caller
b1bcb2
+ * becomes the owner of this buffer, and must free it when it
b1bcb2
+ * is no longer required.
b1bcb2
+ */
b1bcb2
+extern void *grub_buffer_take_data (grub_buffer_t buf);
b1bcb2
+
b1bcb2
+/* Reset this buffer. Note that this does not deallocate any resources. */
b1bcb2
+void grub_buffer_reset (grub_buffer_t buf);
b1bcb2
+
b1bcb2
+/*
b1bcb2
+ * Return a pointer to the underlying data buffer at the specified
b1bcb2
+ * offset from the current read position. Note that this pointer may
b1bcb2
+ * become invalid if the buffer is mutated further.
b1bcb2
+ */
b1bcb2
+static inline void *
b1bcb2
+grub_buffer_peek_data_at (grub_buffer_t buf, grub_size_t off)
b1bcb2
+{
b1bcb2
+  if (grub_add (buf->pos, off, &off))
b1bcb2
+    {
b1bcb2
+      grub_error (GRUB_ERR_OUT_OF_RANGE, N_("overflow is detected."));
b1bcb2
+      return NULL;
b1bcb2
+    }
b1bcb2
+
b1bcb2
+  if (off >= buf->used)
b1bcb2
+    {
b1bcb2
+      grub_error (GRUB_ERR_OUT_OF_RANGE, N_("peek out of range"));
b1bcb2
+      return NULL;
b1bcb2
+    }
b1bcb2
+
b1bcb2
+  return &buf->data[off];
b1bcb2
+}
b1bcb2
+
b1bcb2
+/*
b1bcb2
+ * Return a pointer to the underlying data buffer at the current
b1bcb2
+ * read position. Note that this pointer may become invalid if the
b1bcb2
+ * buffer is mutated further.
b1bcb2
+ */
b1bcb2
+static inline void *
b1bcb2
+grub_buffer_peek_data (grub_buffer_t buf)
b1bcb2
+{
b1bcb2
+  return grub_buffer_peek_data_at (buf, 0);
b1bcb2
+}
b1bcb2
+
b1bcb2
+/* Advance the read position by the specified number of bytes. */
b1bcb2
+extern grub_err_t grub_buffer_advance_read_pos (grub_buffer_t buf, grub_size_t n);
b1bcb2
+
b1bcb2
+#endif /* GRUB_BUFFER_H */