public inbox for u-boot@lists.denx.de
 help / color / mirror / Atom feed
From: Lukasz Majewski <lukma@denx.de>
To: u-boot@lists.denx.de
Subject: [U-Boot] [PATCH 2/3] cmd: part: Extract common code to separate function
Date: Wed, 28 Feb 2018 10:25:52 +0100	[thread overview]
Message-ID: <20180228102552.53c2ac2a@jawa> (raw)
In-Reply-To: <20180226211801.16060-3-semen.protsenko@linaro.org>

On Mon, 26 Feb 2018 23:18:00 +0200
Sam Protsenko <semen.protsenko@linaro.org> wrote:

> Refactor the code for "part start" and "part size" commands to avoid
> code duplication.
> 
> Signed-off-by: Sam Protsenko <semen.protsenko@linaro.org>
> ---
>  cmd/part.c | 61
> +++++++++++++++++++++++-------------------------------------- 1 file
> changed, 23 insertions(+), 38 deletions(-)
> 
> diff --git a/cmd/part.c b/cmd/part.c
> index fd8825a7ec..ec791fdc5d 100644
> --- a/cmd/part.c
> +++ b/cmd/part.c
> @@ -22,6 +22,11 @@
>  #include <part.h>
>  #include <vsprintf.h>
>  
> +enum cmd_part_info {
> +	CMD_PART_INFO_START = 0,
> +	CMD_PART_INFO_SIZE,
> +};
> +
>  static int do_part_uuid(int argc, char * const argv[])
>  {
>  	int part;
> @@ -108,7 +113,7 @@ static int do_part_list(int argc, char * const
> argv[]) return 0;
>  }
>  
> -static int do_part_start(int argc, char * const argv[])
> +static int do_part_info(int argc, char * const argv[], enum
> cmd_part_info param) {
>  	struct blk_desc *desc;
>  	disk_partition_t info;
> @@ -138,7 +143,17 @@ static int do_part_start(int argc, char * const
> argv[]) return 1;
>  	}
>  
> -	snprintf(buf, sizeof(buf), LBAF, info.start);
> +	switch (param) {
> +	case CMD_PART_INFO_START:
> +		snprintf(buf, sizeof(buf), LBAF, info.start);
> +		break;
> +	case CMD_PART_INFO_SIZE:
> +		snprintf(buf, sizeof(buf), LBAF, info.size);
> +		break;
> +	default:
> +		printf("** Unknown cmd_part_info value: %d\n",
> param);
> +		return 1;
> +	}
>  
>  	if (argc > 3)
>  		env_set(argv[3], buf);
> @@ -148,44 +163,14 @@ static int do_part_start(int argc, char * const
> argv[]) return 0;
>  }
>  
> -static int do_part_size(int argc, char * const argv[])
> +static int do_part_start(int argc, char * const argv[])
>  {
> -	struct blk_desc *desc;
> -	disk_partition_t info;
> -	char buf[512] = { 0 };
> -	char *endp;
> -	int part;
> -	int err;
> -	int ret;
> -
> -	if (argc < 3)
> -		return CMD_RET_USAGE;
> -	if (argc > 4)
> -		return CMD_RET_USAGE;
> -
> -	ret = blk_get_device_by_str(argv[0], argv[1], &desc);
> -	if (ret < 0)
> -		return 1;
> -
> -	part = simple_strtoul(argv[2], &endp, 0);
> -	if (*endp == '\0') {
> -		err = part_get_info(desc, part, &info);
> -		if (err)
> -			return 1;
> -	} else {
> -		part = part_get_info_by_name(desc, argv[2], &info);
> -		if (part == -1)
> -			return 1;
> -	}
> -
> -	snprintf(buf, sizeof(buf), LBAF, info.size);
> -
> -	if (argc > 3)
> -		env_set(argv[3], buf);
> -	else
> -		printf("%s\n", buf);
> +	return do_part_info(argc, argv, CMD_PART_INFO_START);
> +}
>  
> -	return 0;
> +static int do_part_size(int argc, char * const argv[])
> +{
> +	return do_part_info(argc, argv, CMD_PART_INFO_SIZE);
>  }
>  
>  static int do_part(cmd_tbl_t *cmdtp, int flag, int argc, char *
> const argv[])

Reviewed-by: Lukasz Majewski <lukma@denx.de>


Best regards,

Lukasz Majewski

--

DENX Software Engineering GmbH,      Managing Director: Wolfgang Denk
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 488 bytes
Desc: OpenPGP digital signature
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20180228/0fb00d71/attachment.sig>

  reply	other threads:[~2018-02-28  9:25 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-02-26 21:17 [U-Boot] [PATCH 0/3] Provide partition names to commands in environment Sam Protsenko
2018-02-26 21:17 ` [U-Boot] [PATCH 1/3] cmd: part: Allow passing partition name to start and size Sam Protsenko
2018-02-28  9:25   ` Lukasz Majewski
2018-03-14 14:09   ` [U-Boot] [U-Boot, " Tom Rini
2018-02-26 21:18 ` [U-Boot] [PATCH 2/3] cmd: part: Extract common code to separate function Sam Protsenko
2018-02-28  9:25   ` Lukasz Majewski [this message]
2018-03-14 14:09   ` [U-Boot] [U-Boot, " Tom Rini
2018-02-26 21:18 ` [U-Boot] [PATCH 3/3] env: ti: boot: Get rid of magic numbers Sam Protsenko
2018-03-14 14:09   ` [U-Boot] [U-Boot,3/3] " Tom Rini

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20180228102552.53c2ac2a@jawa \
    --to=lukma@denx.de \
    --cc=u-boot@lists.denx.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox