[PATCH 6/6] cmd: gpt: Add command to swap partition order

Simon Glass sjg at google.com
Thu Aug 24 01:57:51 CEST 2023


Hi Joshua,

On Tue, 15 Aug 2023 at 10:28, Joshua Watt <jpewhacker at gmail.com> wrote:
>
> Adds a command called "gpt swap-postition" which will swap the order two
> partitions are listed in the GPT partition table (but leaves them
> pointing to the same locations on disk).
>
> Signed-off-by: Joshua Watt <JPEWhacker at gmail.com>
> ---
>  cmd/gpt.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++--
>  1 file changed, 48 insertions(+), 2 deletions(-)
>
> diff --git a/cmd/gpt.c b/cmd/gpt.c
> index 58564436d3..c8a2b5ae7b 100644
> --- a/cmd/gpt.c
> +++ b/cmd/gpt.c
> @@ -859,7 +859,7 @@ static int do_rename_gpt_parts(struct blk_desc *dev_desc, char *subcomm,
>         int partlistlen, ret, numparts = 0, partnum, i = 1, ctr1 = 0, ctr2 = 0;
>
>         if ((subcomm == NULL) || (name1 == NULL) || (name2 == NULL) ||
> -           (strcmp(subcomm, "swap") && (strcmp(subcomm, "rename"))))
> +           (strcmp(subcomm, "swap") && strcmp(subcomm, "rename") && strcmp(subcomm, "swap-position")))

While you are here you could tidy this code:

if (!subcomm || !name1 || !name2 ||
...

>                 return -EINVAL;
>
>         ret = get_disk_guid(dev_desc, disk_guid);
> @@ -920,6 +920,48 @@ static int do_rename_gpt_parts(struct blk_desc *dev_desc, char *subcomm,
>                         ret = -EINVAL;
>                         goto out;
>                 }
> +       } else if(!strcmp(subcomm, "swap-position")) {
> +               int idx1, idx2;
> +               struct disk_partition first, second;

blank line

> +               idx1 = simple_strtoul(name1, NULL, 10);
> +               if (idx1 <= 0 || idx1 > numparts) {
> +                       printf("Illegal partition number %s\n", name1);
> +                       ret = -EINVAL;
> +                       goto out;
> +               }
> +               idx2 = simple_strtoul(name2, NULL, 10);
> +               if (idx2 <= 0 || idx2 > numparts) {
> +                       printf("Illegal partition number %s\n", name2);
> +                       ret = -EINVAL;
> +                       goto out;
> +               }
> +               if (idx1 == idx2) {
> +                       printf("Cannot swap partition with itself\n");
> +                       ret = -EINVAL;
> +                       goto out;
> +               }
> +
> +               i = 1;
> +               list_for_each(pos, &disk_partitions) {
> +                       curr = list_entry(pos, struct disk_part, list);
> +                       if (i == idx1) {
> +                               first = curr->gpt_part_info;
> +                       } else if (i == idx2) {
> +                               second = curr->gpt_part_info;
> +                       }

Can you please either use patman or manually checkpatch on your
patches? This should not have {}

> +                       i++;
> +               }
> +
> +               i = 1;
> +               list_for_each(pos, &disk_partitions) {
> +                       curr = list_entry(pos, struct disk_part, list);
> +                       if (i == idx1) {
> +                               curr->gpt_part_info = second;
> +                       } else if (i == idx2) {
> +                               curr->gpt_part_info = first;
> +                       }
> +                       i++;
> +               }
>         } else { /* rename */
>                 if (strlen(name2) > PART_NAME_LEN) {
>                         printf("Names longer than %d characters are truncated.\n", PART_NAME_LEN);
> @@ -1122,7 +1164,8 @@ static int do_gpt(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
>         } else if (strcmp(argv[1], "read") == 0) {
>                 ret = do_get_gpt_info(blk_dev_desc, (argc == 5) ? argv[4] : NULL);
>         } else if ((strcmp(argv[1], "swap") == 0) ||
> -                  (strcmp(argv[1], "rename") == 0)) {
> +                  (strcmp(argv[1], "rename") == 0) ||
> +                  (strcmp(argv[1], "swap-position") == 0)) {
>                 ret = do_rename_gpt_parts(blk_dev_desc, argv[1], argv[4], argv[5]);
>  #endif
>         } else if ((strcmp(argv[1], "set-bootable") == 0)) {
> @@ -1175,11 +1218,14 @@ U_BOOT_CMD(gpt, CONFIG_SYS_MAXARGS, 1, do_gpt,
>         " gpt swap <interface> <dev> <name1> <name2>\n"
>         "    - change all partitions named name1 to name2\n"
>         "      and vice-versa\n"
> +       " gpt swap-position <interface> <dev> <part1> <part2>\n"
> +       "    - Swap the order of name1 with name2 in the partition table\n"
>         " gpt rename <interface> <dev> <part> <name>\n"
>         "    - rename the specified partition\n"
>         " Example usage:\n"
>         " gpt swap mmc 0 foo bar\n"
>         " gpt rename mmc 0 3 foo\n"
> +       " gpt swap-partitions mmc 0 1 2\n"
>  #endif
>         " gpt set-bootable <interface> <dev> <list>\n"
>         "    - make partition names in list bootable\n"
> --
> 2.33.0
>

Regards,
Simon


More information about the U-Boot mailing list