Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1lAUd5-0006pF-U0 for mharc-grub-devel@gnu.org; Fri, 12 Feb 2021 04:20:47 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:51440) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lAUd4-0006o6-Pd for grub-devel@gnu.org; Fri, 12 Feb 2021 04:20:46 -0500 Received: from mout.gmx.net ([212.227.15.15]:59419) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lAUd1-000425-JV for grub-devel@gnu.org; Fri, 12 Feb 2021 04:20:46 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.net; s=badeba3b8450; t=1613121641; bh=SpH8kJAhNIpMfvJvEP62iWcE3YoleqaFo6YEmPrye3g=; h=X-UI-Sender-Class:Subject:From:To:Reply-To:References:Date: In-Reply-To; b=hsvJYT/lF7MiLcb+o7/YZpK/wnsX+hgbYy6IAKmj5hhVrH0cFb/2wrefjU9SY2lY6 Jv08m0j2xgI5MLmwWIWYJZm5uyM1/niSgKlwWDm9AWU51iW71dibAyp7dm3ArTATOZ JvNAyLfLyDt8schEd5XGFyzAqSoK170SJH6Ff2CY= X-UI-Sender-Class: 01bb95c1-4bf8-414a-932a-4f6e2808ef9c Received: from [172.30.9.7] ([131.228.32.167]) by mail.gmx.net (mrgmx004 [212.227.17.190]) with ESMTPSA (Nemesis) id 1MsHns-1m30Bn0pKc-00thOV for ; Fri, 12 Feb 2021 10:20:41 +0100 Subject: Re: [PATCH v2 2/2] efi: SPI NOR flash command line From: Michael Lawnick To: The development of GNU GRUB Reply-To: The development of GNU GRUB References: Message-ID: Date: Fri, 12 Feb 2021 10:20:40 +0100 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Thunderbird/78.7.1 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: quoted-printable X-Provags-ID: V03:K1:EqIfwZip9FVTV11ve4MIVIxMnxmzkuILnLcNUAihAdGEqmOoXuf TUpZk+LnbM7cubca/UMFNtzujXqCsTCX5pPXCfK5va2Y9hVYvwYJpmv2KY86O1wXxl3cgER vM+oboaCpSAb6aexUOzDN05i7NjW1uFu2WoPNO0ysLjIPX8C6pq4premcOwMo8SgrKMrU+V wXOD2oVVyKSYdIUMwv1gQ== X-UI-Out-Filterresults: notjunk:1;V03:K0:urZeWuMGfQM=:4aRJeC2C//ii8QHB8RQpes DqeRIUP0Jyf7pt2qix5LdHq9Nz6q0Y1NhZCsk/FMjoOAiyuC5HrSpyzkVDtgn/3qaRBLrTBKe knv7yOImXDVPKffPr4ie8YOjIG4hKJ/EGEHR3Jhsgj15qnnutJPs4aSAOdLslYQHC5+7Kt+I5 eSvmdZ839ZFk5cPs+JsfeMl0KkVkzVa9COxfC5D15D74ufGRISv7QdO0wfAOQEcDCImbfob/K 2Y3iYwkSRkzV3B+2bKJWFWjab0QZNhOuZesd1jZlrN7ipdrYGEv2FuhiS30hsHvxWaJjkzbQe avc4t8WxfKsHRtBenwArpFf5QTzrgK38dSWU/gkv4YMlZHo+dvLRB1T9EvuTepFrF/blt7oPV z1iqAFGtZh+rB+zb26LwhFkvpKuLJSFIM2+WRkY2tCuQrSa9XaKkPwPmQ9eDojcc65qQtDpwM kjmQMX/sI/G8V0TMeRQRy9qeGMC8aKvsw1U/RNyKQKovLG+3U0XNFsJ30tNHeAsX6LIyh08VJ 5wXvlUJTOZcoYaJBmb3cNuuRHqS3i8OyunqVvU8zPRQqrzUOrS6VRArxbfyA1A2PmZUQBQ5kr Wc06BYW22lP8NTr6Cy+cvzBzEkvrvtrLI8J4J81P6r32QkRZHiEEn9GTnjbkuySgYrVHK30B0 uq5T5JlYBtFhgSvahPlz5qg/s2+KkZDqeUTp95Sd3lVBB4JE7NK4zjCCVLrt71dQzs8w8yyrj 0ed52Uc0UjwrZxxAI4Xe0AZGp+HlHg0mJnsMPaSEhAk2P5XD7u/DhccDekxoVxYKgvpkNYQcA QY2bAdvlycDaJIrLeWVc4bvCf/Yjrny1nfv1aTVK4osPkUxtcjCJij7QAlmFGgQOSeZdHAybG 0yUIGprVrWhwec18Z1WQ== Received-SPF: pass client-ip=212.227.15.15; envelope-from=ml.lawnick@gmx.de; helo=mout.gmx.net X-Spam_score_int: -26 X-Spam_score: -2.7 X-Spam_bar: -- X-Spam_report: (-2.7 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, FREEMAIL_FROM=0.001, NICE_REPLY_A=-0.119, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 12 Feb 2021 09:20:47 -0000 Note: There is a bug in code below which will let this code fail on BIOS-UEFI and requires a v3 as soon as basic discussion whether this sort of functionality is accepted at all is done. I don't want to disturb thread by a v3 thread. KR Michael Am 05.02.2021 um 10:02 schrieb Michael Lawnick: > Add SPI NOR flash to command line > Based on patch '[PATCH 1/2] efi: SPI NOR flash support' > add command line functionality for interactive access > to SPI NOR flash. > > Supported commands: > spi_nor > init - establish communication to a flash part > read - read from flash part to memory or print hexdump > write - write to flash part from memory > erase - erase some erase blocks > > Signed-off-by: Michael Lawnick > --- > [Patch v2 2/2]: no change > --- > diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def > index 4d775e5f6..403a5432f 100644 > --- a/grub-core/Makefile.core.def > +++ b/grub-core/Makefile.core.def > @@ -659,6 +659,7 @@ module =3D { > module =3D { > name =3D efi_spi_nor; > common =3D bus/spi/efi_spi_nor.c; > + common =3D commands/efi/spinorcmd.c; > enable =3D efi; > }; > > diff --git a/grub-core/commands/efi/spinorcmd.c > b/grub-core/commands/efi/spinorcmd.c > new file mode 100644 > index 000000000..c55a900aa > --- /dev/null > +++ b/grub-core/commands/efi/spinorcmd.c > @@ -0,0 +1,253 @@ > +/* spinorcmd.c - Give access to SPI NOR flash on command line. > + * Copyright 2021 Nokia > + * Licensed under the GNU General Public License v3.0 only > + * SPDX-License-Identifier: GPL-3.0-only > + * > + * GRUB -- GRand Unified Bootloader > + * Copyright (C) 2008 Free Software Foundation, Inc. > + * > + * GRUB is free software: you can redistribute it and/or modify > + * it under the terms of the GNU General Public License as published b= y > + * the Free Software Foundation, either version 3 of the License, or > + * (at your option) any later version. > + * > + * GRUB 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 GRUB. If not, see . > + */ > +#include > +#include > +#include > +#include > +#include > +#include > +#include > + > +GRUB_MOD_LICENSE ("GPLv3+"); > + > +static void *cli_spi_flash_device =3D NULL; > + > +static void > +usage (void) > +{ > + grub_printf("spi_nor - access SPI NOR flash through UEFI API\n"); > + grub_printf("Usage:\n"); > + grub_printf(" spi_nor init|read|write|format \n"); > + grub_printf(" init [-id ] [-cnt ]\n"); > + grub_printf(" to be called once before operation on a > device.\n"); > + grub_printf(" : optional up to 3 bytes flash > identifier\n"); > + grub_printf(" to match against\n"); > + grub_printf(" : use n-th occurance of device\n"); > + grub_printf(" (can be combined with )\n"); > + grub_printf(" read []\n"); > + grub_printf(" read and dump/save bytes\n"); > + grub_printf(" write \n"); > + grub_printf(" write bytes from to flash\n"); > + grub_printf(" erase \n"); > + grub_printf(" format area \n"); > + grub_printf(" ( and must be erase block > alligned)\n"); > + grub_printf("\n"); > +} > + > +/* get_init_args - return index and number of -id and/or -cnt argument= s > + handle 4 possible inputs: > + - spi_nor init -id -cnt > + - spi_nor init -cnt -id > + - spi_nor init -cnt > + - spi_nor init -id > +*/ > +static int get_init_args(int argc, char **args, int *id_idx, int > *id_cnt, int *cnt_idx) > +{ > + int opt_idx =3D 1; > + *id_idx =3D 0; > + *cnt_idx =3D 0; > + *id_cnt =3D 0; > + > + while (opt_idx < argc) { > + if (!grub_strcmp(args[opt_idx],"-id")) { > + opt_idx++; > + *id_idx =3D opt_idx; > + > + while ((opt_idx < argc) > + && (grub_strcmp(args[opt_idx], "-cnt")) > + && (*id_cnt < 3)) { > + opt_idx++; > + *id_cnt =3D *id_cnt + 1; > + } > + > + if (*id_cnt =3D=3D 0) > + return 1; > + } else if (!grub_strcmp(args[opt_idx],"-cnt")) { > + if (argc > opt_idx + 1) { > + *cnt_idx =3D opt_idx + 1; > + opt_idx +=3D 2; > + } else { > + return 1; > + } > + } else { > + return 1; > + } > + } > + > + return 0; > +} > + > +static grub_err_t > +grub_cmd_spi_nor (grub_command_t cmd __attribute__ ((unused)), > + int argc __attribute__ ((unused)), > + char **args __attribute__ ((unused))) > +{ > + grub_err_t ret; > + int cnt_idx, id_idx, id_cnt =3D 0; > + grub_uint8_t *device_id =3D NULL, devId[3]; > + grub_uint32_t instance =3D 0; > + > + if (argc < 1) { > + grub_printf("Missing argument\n"); > + usage(); > + return GRUB_ERR_BAD_ARGUMENT; > + } > + > + if (!grub_strcmp("init", args[0])) { > + if (argc !=3D 1) { > + if (get_init_args(argc, args, &id_idx, &id_cnt, &cnt_idx)) { > + usage(); > + return GRUB_ERR_BAD_ARGUMENT; > + } else { > + if (id_idx !=3D 0) { > + for (int i=3D0; i + devId[i] =3D grub_strtoul(args[id_idx + i], NULL, 0); > + device_id =3D devId; > + } > + if (cnt_idx !=3D 0) { > + instance =3D grub_strtoul(args[cnt_idx], NULL, 0); > + } > + } > + } > + > + cli_spi_flash_device =3D grub_efi_spi_nor_init(device_id, id_cnt, > instance); > + if (cli_spi_flash_device =3D=3D NULL) { > + grub_printf("No SPI NOR flash found\n"); > + return GRUB_ERR_UNKNOWN_DEVICE; > + } > + grub_printf("Found dev %x, capacity 0x%x bytes, erase block size 0x%x= \n", > + grub_efi_spi_nor_device_id(cli_spi_flash_device), > + grub_efi_spi_nor_flash_size(cli_spi_flash_device), > + grub_efi_spi_nor_erase_block_size(cli_spi_flash_device)); > + return GRUB_ERR_NONE; > + } > + > + if (cli_spi_flash_device =3D=3D NULL) { > + grub_printf("No known device. Call 'init' first\n"); > + usage(); > + return GRUB_ERR_UNKNOWN_DEVICE; > + } > + > + if (!grub_strcmp("read", args[0])) { > + grub_uint8_t *data; > + grub_uint32_t offset, num_bytes, i, j; > + > + if (argc < 3) { > + grub_printf("Missing parameters\n"); > + usage(); > + return GRUB_ERR_BAD_ARGUMENT; > + } > + > + offset =3D grub_strtoul(args[1], NULL, 0); > + num_bytes =3D grub_strtoul(args[2], NULL, 0); > + > + if (argc =3D=3D 4) { > + data =3D (grub_uint8_t *)grub_strtoul(args[3], NULL, 0); > + if (data =3D=3D NULL) { > + grub_printf("Bad memory pointer, 0 not supported.\n"); > + usage(); > + return GRUB_ERR_BAD_ARGUMENT; > + } > + } else { > + data =3D grub_malloc(num_bytes); > + if (data =3D=3D NULL) { > + grub_printf("Out of memory.\n"); > + usage(); > + return GRUB_ERR_OUT_OF_MEMORY; > + } > + } > + > + ret =3D grub_efi_spi_nor_read(cli_spi_flash_device, data, offset, > num_bytes); > + if (ret !=3D GRUB_ERR_NONE) > + return ret; > + > + if (argc =3D=3D 3) { > + for (i=3D0; i + grub_printf("0x%06x: ", i + offset); > + for (j=3D0; (j<16) && (i+j + grub_printf("%02x ", data[i+j]); > + grub_printf("\n"); > + } > + grub_free(data); > + } > + return GRUB_ERR_NONE; > + } > + > + if (!grub_strcmp("write", args[0])) { > + grub_uint8_t *data; > + grub_uint32_t offset, num_bytes; > + > + if (argc !=3D 4) { > + grub_printf("Wrong number of parameters\n"); > + usage(); > + return GRUB_ERR_BAD_ARGUMENT; > + } > + > + offset =3D grub_strtoul(args[1], NULL, 0); > + num_bytes =3D grub_strtoul(args[2], NULL, 0); > + > + data =3D (grub_uint8_t *)grub_strtoul(args[3], NULL, 0); > + if (data =3D=3D NULL) { > + grub_printf("Bad memory pointer, 0 not supported.\n"); > + usage(); > + return GRUB_ERR_BAD_ARGUMENT; > + } > + > + ret =3D grub_efi_spi_nor_write(cli_spi_flash_device, data, offset, > num_bytes); > + > + return ret; > + } > + > + if (!grub_strcmp("erase", args[0])) { > + grub_uint32_t offset, num_bytes; > + > + if (argc !=3D 3) { > + grub_printf("Wrong number of parameters\n"); > + usage(); > + return GRUB_ERR_BAD_ARGUMENT; > + } > + > + offset =3D grub_strtoul(args[1], NULL, 0); > + num_bytes =3D grub_strtoul(args[2], NULL, 0); > + > + ret =3D grub_efi_spi_nor_erase(cli_spi_flash_device, offset, num_byte= s); > + > + return ret; > + } > + > + grub_printf("Unknown command \"%s\"\n", args[1]); > + usage(); > + return GRUB_ERR_BAD_ARGUMENT; > +} > + > +static grub_command_t cmd; > + > +GRUB_MOD_INIT(spinorcmd) > +{ > + cmd =3D grub_register_command("spi_nor", grub_cmd_spi_nor, > + "", "access SPI NOR flash"); > +} > + > +GRUB_MOD_FINI(spinorcmd) > +{ > + grub_unregister_command(cmd); > +} > > > _______________________________________________ > Grub-devel mailing list > Grub-devel@gnu.org > https://lists.gnu.org/mailman/listinfo/grub-devel >