From patchwork Fri Nov 9 10:48:06 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Piotr Wilczek X-Patchwork-Id: 198018 X-Patchwork-Delegate: trini@ti.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from theia.denx.de (theia.denx.de [85.214.87.163]) by ozlabs.org (Postfix) with ESMTP id 782292C0319 for ; Fri, 9 Nov 2012 21:49:21 +1100 (EST) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 63EAE4A901; Fri, 9 Nov 2012 11:49:07 +0100 (CET) X-Virus-Scanned: Debian amavisd-new at theia.denx.de Received: from theia.denx.de ([127.0.0.1]) by localhost (theia.denx.de [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id aoLY6VkvtZxT; Fri, 9 Nov 2012 11:49:07 +0100 (CET) Received: from theia.denx.de (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 7D6E64A917; Fri, 9 Nov 2012 11:48:57 +0100 (CET) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id D994D4A907 for ; Fri, 9 Nov 2012 11:48:53 +0100 (CET) X-Virus-Scanned: Debian amavisd-new at theia.denx.de Received: from theia.denx.de ([127.0.0.1]) by localhost (theia.denx.de [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id mu92U4mFxNBq for ; Fri, 9 Nov 2012 11:48:52 +0100 (CET) X-policyd-weight: NOT_IN_SBL_XBL_SPAMHAUS=-1.5 NOT_IN_SPAMCOP=-1.5 NOT_IN_BL_NJABL=-1.5 (only DNSBL check requested) Received: from mailout4.samsung.com (mailout4.samsung.com [203.254.224.34]) by theia.denx.de (Postfix) with ESMTP id D82CE4A8E7 for ; Fri, 9 Nov 2012 11:48:45 +0100 (CET) Received: from epcpsbgm1.samsung.com (epcpsbgm1 [203.254.230.26]) by mailout4.samsung.com (Oracle Communications Messaging Server 7u4-24.01(7.0.4.24.0) 64bit (built Nov 17 2011)) with ESMTP id <0MD7002XHVCYKGE0@mailout4.samsung.com> for u-boot@lists.denx.de; Fri, 09 Nov 2012 19:48:43 +0900 (KST) X-AuditID: cbfee61a-b7fa66d0000004cf-9e-509cdf8b4cf1 Received: from epmmp1.local.host ( [203.254.227.16]) by epcpsbgm1.samsung.com (EPCPMTA) with SMTP id 1C.4B.01231.B8FDC905; Fri, 09 Nov 2012 19:48:43 +0900 (KST) Received: from mcdsrvbld02.digital.local ([106.116.37.23]) by mmp1.samsung.com (Oracle Communications Messaging Server 7u4-24.01 (7.0.4.24.0) 64bit (built Nov 17 2011)) with ESMTPA id <0MD7001C5VCNZMC0@mmp1.samsung.com> for u-boot@lists.denx.de; Fri, 09 Nov 2012 19:48:43 +0900 (KST) From: Piotr Wilczek To: u-boot@lists.denx.de Date: Fri, 09 Nov 2012 11:48:06 +0100 Message-id: <1352458087-20462-3-git-send-email-p.wilczek@samsung.com> X-Mailer: git-send-email 1.7.10 In-reply-to: <1352458087-20462-1-git-send-email-p.wilczek@samsung.com> References: <1352452938-2375-1-git-send-email-p.wilczek@samsung.com> <1352458087-20462-1-git-send-email-p.wilczek@samsung.com> X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFnrMJMWRmVeSWpSXmKPExsVy+t9jAd3u+3MCDCbflLZ4u7eT3YHR4+yd HYwBjFFcNimpOZllqUX6dglcGctnVxa8cq841tzN2MC41KKLkZNDQsBE4sjujYwQtpjEhXvr 2boYuTiEBBYxSpw4dogVwlnMJHH5ySF2kCo2AW2J5W/egHWICEhI/Oq/yghSxCzwmEliyqZ7 zCAJYQEriYZbl8BsFgFVia3bHjJ1MXJw8Aq4Svx6lguxTV7i6f0+NhCbU8BNYlvrcWaIZQ2M Ei8efGSbwMi7gJFhFaNoakFyQXFSeq6hXnFibnFpXrpecn7uJkaw159J7WBc2WBxiFGAg1GJ hzfxwewAIdbEsuLK3EOMEhzMSiK8RmfnBAjxpiRWVqUW5ccXleakFh9ilOZgURLnbfZICRAS SE8sSc1OTS1ILYLJMnFwSjUwrvLJsd3z7FC10Jmd/a6Brs2JPNbT6/VWrdGxz/V45rzimX7Q vhOfW+5y9Fb8Ypxd5X8h6ckmv30xzekvbQqEzVsusE0/vXous+2PXdXhiZeZ6w9+qJphyvJM kmfCJd8LeQE7Lqy/xViZzqj/QoT55s5e/t9FXdzNMYw1+ickUo4+UI21u/5EiaU4I9FQi7mo OBEAJ7bQ1fYBAAA= Cc: Stephen Warren , Kyungmin Park , Piotr Wilczek , Tom Rini Subject: [U-Boot] [PATCH v4 6/7] gpt: Support for new "gpt" command X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.11 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: u-boot-bounces@lists.denx.de Errors-To: u-boot-bounces@lists.denx.de New command - "gpt" is supported. It restores the GPT partition table. It looks into the "partitions" environment variable for partitions definition. It can be enabled at target configuration file with CONFIG_CMD_GPT. Simple UUID generator has been implemented. It uses the the gd->start_addr_sp for entrophy pool. Moreover the pool address is used as crc32 seed. Signed-off-by: Lukasz Majewski Signed-off-by: Piotr Wilczek Signed-off-by: Kyungmin Park --- Changes for v2: - gpt command now accepts device medium and its number (e.g. gpt mmc 0) - UUIDs can be passed via u-boot prompt when used with gpt command - Format of restored GPT has been changed - now key=value pairs are used 'name="PARTS_CSA",size=8MiB;\ ' 'name="PARTS_BOOTLOADER",size=60MiB; \' - guid_gen now accepts "pool" pointer and guid pointer - gd->start_addr_sp is used as a primary source of entrophy - static buffers definitions have been removed - remove memsize_to_blocks function with call to standard ustrtoul - doxygen comments for functions added Changes for v3: - Remove unnecessary braces Changes for v4: - partions list can be passed as environment variable or directly as text - each value in partions list can be passed as environment variable --- common/Makefile | 1 + common/cmd_gpt.c | 300 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 301 insertions(+), 0 deletions(-) create mode 100644 common/cmd_gpt.c diff --git a/common/Makefile b/common/Makefile index 9e43322..741781f 100644 --- a/common/Makefile +++ b/common/Makefile @@ -192,6 +192,7 @@ COBJS-$(CONFIG_MODEM_SUPPORT) += modem.o COBJS-$(CONFIG_UPDATE_TFTP) += update.o COBJS-$(CONFIG_USB_KEYBOARD) += usb_kbd.o COBJS-$(CONFIG_CMD_DFU) += cmd_dfu.o +COBJS-$(CONFIG_CMD_GPT) += cmd_gpt.o endif ifdef CONFIG_SPL_BUILD diff --git a/common/cmd_gpt.c b/common/cmd_gpt.c new file mode 100644 index 0000000..171eeb3 --- /dev/null +++ b/common/cmd_gpt.c @@ -0,0 +1,300 @@ +/* + * cmd_gpt.c -- GPT (GUID Partition Table) handling command + * + * Copyright (C) 2012 Samsung Electronics + * author: Lukasz Majewski + * author: Piotr Wilczek + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include +#include +#include +#include +#include +#include + +/** + * extract_env(): Convert string from '&{env_name}' to 'env_name' + * + * @param p - pointer to string + * + * @return - zero on success + */ +static int extract_env(char *p) +{ + char *p1, *p2; + + if (!p) + return -1; + + p1 = strstr(p, "${"); + p2 = strstr(p, "}"); + + if (p1 && p2) { + *p2 = '\0'; + memmove(p, p+2, p2-p1-1); + } else { + return -2; + } + + return 0; +} + +/** + * extract_val(): Extract value from a key=value pair + * + * @param p - pointer to string + * @param tab - table to store extracted value + * @param i - actual tab element to work on + * @param key - extract only given key + * + * @return - zero on success; otherwise error + */ +static int extract_val(char **p, char *tab[], int i, char *key) +{ + char *t, *e, *tok = *p; + char *k; + + t = strsep(&tok, ","); + k = t; + strsep(&t, "="); + + if (key && strcmp(k, key)) + return -2; + + if (extract_env(t) == 0) { + e = getenv(t); + if (e == NULL) + return -1; + t = e; + } + + tab[i] = calloc(strlen(t) + 1, 1); + if (tab[i] == NULL) { + printf("%s: calloc failed!\n", __func__); + return -1; + } + strcpy(tab[i], t); + *p = tok; + + return 0; +} + +/** + * set_gpt_info(): Fill gpt information for GPT header and partition entries + * + * @param dev_desc - block device descriptor + * @param str_uuid - string uuid representation + * + * @return - zero on success; otherwise error + * + */ +static int set_gpt_info(block_dev_desc_t *dev_desc, char *str_part, + disk_partition_t *partitions[], const int parts_count) +{ + char *ps[parts_count]; + char *name[parts_count]; + char *uuid[parts_count]; + unsigned int size[parts_count]; + char *tok, *p, *s, *ss; + int i, ret; + + debug("%s: MMC lba num: 0x%x %d\n", __func__, + (unsigned int)dev_desc->lba, (unsigned int)dev_desc->lba); + + if (!str_part) + return -2; + ret = 0; + + s = str_part; + + printf("PARTITIONS: %s\n", s); + ss = calloc(strlen(s) + 1, 1); + if (ss == NULL) { + printf("%s: calloc failed!\n", __func__); + return -1; + } + memcpy(ss, s, strlen(s) + 1); + + for (i = 0, p = ss; i < parts_count; i++) { + tok = strsep(&p, ";"); + if (tok == NULL) + break; + + if (extract_val(&tok, name, i, "name")) { + ret = -1; + goto err; + } + + if (extract_val(&tok, ps, i, "size")) { + ret = -1; + free(name[i]); + goto err; + } + + if (extract_val(&tok, uuid, i, "uuid")) { + /* uuid string length equals 37 */ + uuid[i] = calloc(37, 1); + if (uuid[i] == NULL) { + printf("%s: calloc failed!\n", __func__); + ret = -1; + free(name[i]); + free(ps[i]); + goto err; + } + } + } + + printf("found %d partitions\n", i); + + for (i = 0; i < parts_count; i++) { + p = ps[i]; + size[i] = ustrtoul(p, &p, 0); + size[i] /= dev_desc->blksz; + } + + for (i = 0; i < parts_count; i++) { + partitions[i]->size = size[i]; + partitions[i]->blksz = dev_desc->blksz; + strncpy((char *)&partitions[i]->name, name[i], + sizeof(partitions[i]->name)); +#ifdef CONFIG_PARTITION_UUIDS + strncpy((char *)&partitions[i]->uuid, uuid[i], + sizeof(partitions[i]->uuid)); +#endif + } + + i = parts_count; + err: + for (i--; i >= 0; i--) { + free(name[i]); + free(uuid[i]); + free(ps[i]); + } + + free(ss); + return ret; +} + +static int gpt_mmc_default(int dev, char *str_part) +{ + int i; + u8 part_count; + char *s , *p; + + struct mmc *mmc = find_mmc_device(dev); + + if (mmc == NULL) { + printf("%s: mmc dev %d NOT available\n", __func__, dev); + return CMD_RET_FAILURE; + } + + puts("Using default GPT UUID\n"); + + /* get partitions list string */ + if (!str_part) +#ifdef DEFAULT_PARTITIONS_ENV + str_part = DEFAULT_PARTITIONS_ENV; +#else + str_part = "partitions"; +#endif + s = getenv(str_part); + if (s == NULL) + s = str_part; + + /* calculate expected number of partitions */ + part_count = 1; + p = s; + while (*p) { + if (*p++ == ';') + part_count++; + } + + /* allocate memory for partitions */ + disk_partition_t *partitions[part_count]; + for (i = 0; i < part_count; i++) { + partitions[i] = calloc(sizeof(disk_partition_t), part_count); + if (partitions[i] == NULL) + goto err; + } + + /* fill partitions */ + if (!set_gpt_info(&mmc->block_dev, s, partitions, part_count)) + /* save partions layout do disk */ + gpt_fill(&mmc->block_dev, partitions, part_count); + else + printf("Partitions list incomplete\n"); + +err: + /* free memory for partitions */ + for (i--; i >= 0; i--) + free(partitions[i]); + + return 0; +} + +/** + * do_gpt(): Perform GPT operations + * + * @param cmdtp - command name + * @param flag + * @param argc + * @param argv + * + * @return zero on success; otherwise error + */ +static int do_gpt(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) +{ + int ret = CMD_RET_SUCCESS; + char *str_part = NULL; + int dev = 0; + + if (argc < 3) + return CMD_RET_USAGE; + + if (argc == 4) { + str_part = strdup(argv[3]); + if (!str_part) { + printf("%s: malloc failed!\n", __func__); + return CMD_RET_FAILURE; + } + } + + if (strcmp(argv[1], "mmc") == 0) { + dev = (int)simple_strtoul(argv[2], NULL, 10); + if (gpt_mmc_default(dev, str_part)) + return CMD_RET_FAILURE; + } + + if (argc == 4) + free(str_part); + + return ret; +} + +U_BOOT_CMD(gpt, CONFIG_SYS_MAXARGS, 1, do_gpt, + "GUID Partition Table", + " \n" + " partions list is in format: name=..,size=..,uuid=..;...\n" + " and can be passed as env or string ex.:\n" + " gpt mmc 0 partitions\n" + " gpt mmc 0 \"name=..,size=..;name=..,size=..;...\"\n" + " gpt mmc 0 \"name=${part1_name},size=..;name=..,size=..;...\"\n" + " - GUID partition table restoration\n" + " Restore GPT information on a device connected\n" + " to interface\n" +);