Re: [PATCH v3 6/6] cmd: Add MBR partition layout control utility

2021-01-16 Thread Tom Rini
On Wed, Dec 23, 2020 at 01:55:15PM +0100, Marek Szyprowski wrote:

> Add a 'mbr' command to let users create or verify MBR partition layout
> based on the provided text description. The partition layout is
> alternatively read from the 'mbr_parts' environment variable. This can be
> used in scripts to help system image flashing tools to ensure proper
> partition layout.
> 
> The syntax of the text description of the partition list is similar to
> the one used by the 'gpt' command. Supported parameters are: name
> (currently ignored), start (partition start offset in bytes), size (in
> bytes or '-' to expand it to the whole free area), bootable (boolean
> flag) and id (MBR partition type). If one wants to create more than 4
> partitions, an 'Extended' primary partition (with 0x05 ID) has to be
> explicitely provided as a one of the first 4 entries.
> 
> Here is an example how to create a 6 partitions (3 on the 'extended
> volume'), some of the predefined sizes:
> 
> > setenv mbr_parts 'name=boot,start=4M,size=128M,bootable,id=0x0e;
>   name=rootfs,size=3072M,id=0x83;
>   name=system-data,size=512M,id=0x83;
>   name=[ext],size=-,id=0x05;
>   name=user,size=-,id=0x83;
>   name=modules,size=100M,id=0x83;
>   name=ramdisk,size=8M,id=0x83'
> > mbr write mmc 0
> 
> Signed-off-by: Marek Szyprowski 

Applied to u-boot/master, thanks!

-- 
Tom


signature.asc
Description: PGP signature


[PATCH v3 6/6] cmd: Add MBR partition layout control utility

2020-12-23 Thread Marek Szyprowski
Add a 'mbr' command to let users create or verify MBR partition layout
based on the provided text description. The partition layout is
alternatively read from the 'mbr_parts' environment variable. This can be
used in scripts to help system image flashing tools to ensure proper
partition layout.

The syntax of the text description of the partition list is similar to
the one used by the 'gpt' command. Supported parameters are: name
(currently ignored), start (partition start offset in bytes), size (in
bytes or '-' to expand it to the whole free area), bootable (boolean
flag) and id (MBR partition type). If one wants to create more than 4
partitions, an 'Extended' primary partition (with 0x05 ID) has to be
explicitely provided as a one of the first 4 entries.

Here is an example how to create a 6 partitions (3 on the 'extended
volume'), some of the predefined sizes:

> setenv mbr_parts 'name=boot,start=4M,size=128M,bootable,id=0x0e;
  name=rootfs,size=3072M,id=0x83;
  name=system-data,size=512M,id=0x83;
  name=[ext],size=-,id=0x05;
  name=user,size=-,id=0x83;
  name=modules,size=100M,id=0x83;
  name=ramdisk,size=8M,id=0x83'
> mbr write mmc 0

Signed-off-by: Marek Szyprowski 
---
 cmd/Kconfig |   8 ++
 cmd/Makefile|   1 +
 cmd/mbr.c   | 314 
 doc/usage/index.rst |   1 +
 doc/usage/mbr.rst   |  93 +
 5 files changed, 417 insertions(+)
 create mode 100644 cmd/mbr.c
 create mode 100644 doc/usage/mbr.rst

diff --git a/cmd/Kconfig b/cmd/Kconfig
index 1595de999b..2c3358e359 100644
--- a/cmd/Kconfig
+++ b/cmd/Kconfig
@@ -1025,6 +1025,14 @@ config CMD_LSBLK
  Print list of available block device drivers, and for each, the list
  of known block devices.
 
+config CMD_MBR
+   bool "MBR (Master Boot Record) command"
+   select DOS_PARTITION
+   select HAVE_BLOCK_DEVICE
+   help
+ Enable the 'mbr' command to ready and write MBR (Master Boot Record)
+ style partition tables.
+
 config CMD_MISC
bool "misc"
depends on MISC
diff --git a/cmd/Makefile b/cmd/Makefile
index dd86675bf2..41379d9a0e 100644
--- a/cmd/Makefile
+++ b/cmd/Makefile
@@ -178,6 +178,7 @@ obj-$(CONFIG_CMD_ZFS) += zfs.o
 
 obj-$(CONFIG_CMD_DFU) += dfu.o
 obj-$(CONFIG_CMD_GPT) += gpt.o
+obj-$(CONFIG_CMD_MBR) += mbr.o
 obj-$(CONFIG_CMD_ETHSW) += ethsw.o
 obj-$(CONFIG_CMD_AXI) += axi.o
 obj-$(CONFIG_CMD_PVBLOCK) += pvblock.o
diff --git a/cmd/mbr.c b/cmd/mbr.c
new file mode 100644
index 00..da2e3a4722
--- /dev/null
+++ b/cmd/mbr.c
@@ -0,0 +1,314 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * cmd_mbr.c -- MBR (Master Boot Record) handling command
+ *
+ * Copyright (C) 2020 Samsung Electronics
+ * author: Marek Szyprowski 
+ *
+ * based on the gpt command.
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+
+/**
+ * extract_val() - Extract a value from the key=value pair list
+ * @str: pointer to string with key=values pairs
+ * @key: pointer to the key to search for
+ *
+ * The list of parameters is come separated, only a value for
+ * the given key is returend.
+ *
+ * Function allocates memory for the value, remember to free!
+ *
+ * Return: Pointer to allocated string with the value.
+ */
+static char *extract_val(const char *str, const char *key)
+{
+   char *v, *k;
+   char *s, *strcopy;
+   char *new = NULL;
+
+   strcopy = strdup(str);
+   if (strcopy == NULL)
+   return NULL;
+
+   s = strcopy;
+   while (s) {
+   v = strsep(, ",");
+   if (!v)
+   break;
+   k = strsep(, "=");
+   if (!k)
+   break;
+   if  (strcmp(k, key) == 0) {
+   new = strdup(v);
+   break;
+   }
+   }
+
+   free(strcopy);
+
+   return new;
+}
+
+/**
+ * found_key() - Search for a key without a value in the parameter list
+ * @str: pointer to string with key
+ * @key: pointer to the key to search for
+ *
+ * The list of parameters is come separated.
+ *
+ * Return: True if key has been found.
+ */
+static bool found_key(const char *str, const char *key)
+{
+   char *k;
+   char *s, *strcopy;
+   bool result = false;
+
+   strcopy = strdup(str);
+   if (!strcopy)
+   return NULL;
+
+   s = strcopy;
+   while (s) {
+   k = strsep(, ",");
+   if (!k)
+   break;
+   if  (strcmp(k, key) == 0) {
+   result = true;
+   break;
+   }
+   }
+
+   free(strcopy);
+
+   return result;
+}
+
+static int str_to_partitions(const char *str_part, int blksz,
+   unsigned long *disk_uuid, struct disk_partition **partitions,
+   int *parts_count)
+{
+   char *tok, *str, *s;
+   int i;
+   char *val, *p;
+   int p_count;
+   struct disk_partition *parts;
+