Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1l7aEk-0005ow-H3 for mharc-grub-devel@gnu.org; Thu, 04 Feb 2021 03:43:38 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:33706) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1l7aEi-0005mx-FN for grub-devel@gnu.org; Thu, 04 Feb 2021 03:43:36 -0500 Received: from mout.gmx.net ([212.227.15.18]:47149) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1l7aEg-000857-5A for grub-devel@gnu.org; Thu, 04 Feb 2021 03:43:36 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.net; s=badeba3b8450; t=1612428211; bh=7ys/s7WyEus+Zs+LHm/Ne1XqtxMEWwp+cGOpr5kn47A=; h=X-UI-Sender-Class:Subject:From:To:Reply-To:References:Date: In-Reply-To; b=OiYshr4adCNeU4+fIprFI+Jmyf4jcfMzbFsoZgeIRASLoL6HmM5tcghwbOBu1v5B0 IgGPks9AbuSiAyLqxfrxMT8yUSt8rcZ8z2WeV8FFrGdr2zmHCrOQPpBBuS35DBDgMF 1AEAumPvqnGkSF8Y6G2xpz39pGos2D5iIiT8Nb74= X-UI-Sender-Class: 01bb95c1-4bf8-414a-932a-4f6e2808ef9c Received: from [172.30.9.6] ([131.228.32.166]) by mail.gmx.net (mrgmx005 [212.227.17.190]) with ESMTPSA (Nemesis) id 1MhlKs-1lcE5L2N16-00doZN for ; Thu, 04 Feb 2021 09:43:31 +0100 Subject: [PATCH 2/2] efi: SPI NOR flash support From: Michael Lawnick To: The development of GNU GRUB Reply-To: The development of GNU GRUB References: Message-ID: <240d421b-6bb2-bb91-45f7-c1b680f8e4ba@gmx.de> Date: Thu, 4 Feb 2021 09:43:30 +0100 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Thunderbird/78.7.0 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:OzeXRHULKFcgFT0m+Ug4XyJKTnYsU6qzN6GuQLFYekQLvq+gdYH YVKhhYimwf8JqSg46nfuLmQJQYkN/i7EZ7aC70+gWphEs77uTi2mOwoWZv5usCxVLxLihva bU9xZ2hd8hE2qYibiOG/HBr/ruHnEMGmUloXLw5rgSoqQ/vIypVYHz/zSm4xXSd1IlXeD2a EWaAFRMWp9hs2WkSgwRTw== X-UI-Out-Filterresults: notjunk:1;V03:K0:30mzEK1jNPQ=:BZqDJ5UkKvbqpwuCNspJ1s e6X1PTg/YIRnSfJecG3eHP4wjwuiuutQwgaQiYuwQ3RhfPel98hbjmrEg57fSmlWBl55IGR4Z rclQvZ0oxV8BC9ujwmuGhnP0IH/PGWk0A6v1Wu+dslg4ea1rxpeAvmwUhzACnSVCHvtS/3Wn+ aUObBUKTVC2pJeWz9asbCEUH3+WNA0IU6A/R9gzuX03ndIlKRPxYWhXTNHjen948nlKSDHQBe pT0tFTIP76niPf5qZmjXTQJoABY4pOAjnIaDVkdVFUK4FI2nEJENAvjcwDbpyfqsh8Tu4OVNL 77m6VzMIHBcxAzxskwKHpazOcOzjeaA9N94p7+5LU98BWzq5lxZwqJYdYTVsJAlyilDI9gMMq t4MvEtY0W1J3B8EvOotMkvzn0Jbn7sPzEwI9X/cJtJi8hRD+nxzQqBzneVcj1ng2GhOyifMpR uDlNhHbS70Ghtxh1LdTqaQSBJ7QDab0sSEPl5EQ+KIBNSOE7SUcFuVQIfU59cSPDNuVAeCKjn E9nt79UyfV4ezFS7nUFhc2wWPhaQa3Mcs52720SggHn2A+JBoy8NUeq8tAGI12Hu6acoGON43 oRZRXMBomGurnOoQoDkSTwmoZX3yp86r8VCq1doEmu9QIhmnwzskm0eINGng9K8zVCeYWLwfS GmTXp90WZ3ZnaGF9SSIK4/D5XLixElr1RBcB3rSqCz1EPOfAmStx3gNWo0PdiEBaeIGfRQHpR Ji8xqY+eeBOuhrcM1XZuZePJB1E/NP9FWW0yy9ZtpOtFn7rGOV5H1lU0Yd6OW1sM37UHTkRLZ yAKvH2l/AJ7vLzGX9wZVXRnUOZ9sR3jM8yZQImzW0YhMFuM6uWxBugYQEDLWUw2rIUb2IlKTd OFzVv3iyZCtJqinFh3kQ== Received-SPF: pass client-ip=212.227.15.18; envelope-from=ml.lawnick@gmx.de; helo=mout.gmx.net X-Spam_score_int: -25 X-Spam_score: -2.6 X-Spam_bar: -- X-Spam_report: (-2.6 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=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: Thu, 04 Feb 2021 08:43:36 -0000 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 =2D-- diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def index 4d775e5f6..403a5432f 100644 =2D-- 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 =2D-- /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 by + * 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 arguments + 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