Blame SOURCES/0464-kern-buffer-Add-variable-sized-heap-buffer.patch

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