Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1l7x17-0000dF-I3 for mharc-grub-devel@gnu.org; Fri, 05 Feb 2021 04:03:06 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:58384) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1l7x16-0000cd-2t for grub-devel@gnu.org; Fri, 05 Feb 2021 04:03:04 -0500 Received: from mout.gmx.net ([212.227.15.15]:35443) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1l7x12-0000Zs-QS for grub-devel@gnu.org; Fri, 05 Feb 2021 04:03:03 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.net; s=badeba3b8450; t=1612515779; bh=DnhjZL2wsLZGYQZVCH5f4uaHgeuvKqw6F1xUhsPal2k=; h=X-UI-Sender-Class:Subject:From:To:Reply-To:References:Date: In-Reply-To; b=J4uPh0CN9N8uzfED4yD5ftcADfSFVhyQ2NdO3LImUv7HkOdqUN0EmwRvcw2mXC7fv UqGXgRlrl4voXRLwpYVEBNcjfVpMOtPAnLvKqFEfrMWajDC4cFkTCozmbRAQ6g30ZN YJjd7sKoKY6cBusaT3q10o9Q5JMaDKONa4Dnnxek= X-UI-Sender-Class: 01bb95c1-4bf8-414a-932a-4f6e2808ef9c Received: from [172.30.9.8] ([131.228.32.168]) by mail.gmx.net (mrgmx005 [212.227.17.190]) with ESMTPSA (Nemesis) id 1Mqb1W-1lljl43sjA-00me1D for ; Fri, 05 Feb 2021 10:02:59 +0100 Subject: [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, 5 Feb 2021 10:02:58 +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:WB/94dSJbaVUYUxFHtgZB5pf7jQig4JmfCZI3X7KG0M5BGiNegh 4uaRPB+zrraR9meeXAkIEyei8Xt9rg5kYQAb0xKkcOaJgQ47lJMuB/exD1YFY1mx24CPELr jMjV3gutx+gecqej2OB2MkWHkHh1HCQsUzajfkUah5y2s3RZEo86OsIYRuJKh295bzgk09s 0inKcXJiYxzMGGDOqpj/g== X-UI-Out-Filterresults: notjunk:1;V03:K0:++DehJvQwew=:dKS7MeKatFlcGOLMc6fKQW LrXjhmLtwWdkcmtqHheuyEu30R0GGLDI/cpqm8aXPyEpy4tewbX6Dr9y39jpUZ42xu4bu3RsF FyjGjn6S0BAEOetUGBw9zx/953xLqZNSeAdNw1NA0kfUDJFSztuilY81md6hvg2+iDAQnGASV YVg7gPbwLTTVtz3f5p30l5tlHzUb/cNTfgabOFWuxBSVdB2XsKu28ukSffH5QCwMsSH8sSBqE eKMRzBlRToYu+qYMZP4E3sESUXIzj4mpx6vorygsNIJFFo9n6bJyg4ruqDS7mq8s7MFVA+ENJ AIbAQkTL3LXU9tfpyrVUCBg+clcxwHZ2L5xi+cfnvr6JYmQwSQbbrbNEfW3J4vIHWRY01ZUIB Vz536nR1OLGNd9guOzaWG+J/vhIB3+OnLIwgXDbDc/0nKawxj55DEXhp6LbSpV7sthAD7pXxs Qib92/fwbvLBCqkDS9u6OERjaoQUwTC7eA4BgRTsReGvSTTLTFNo7x41BcmyCcw7L7IyC5+ML aKyd9lf+M4jze7QUjPAMA12kBkmWLsgKLy+F75pthwBghHEw//JLAlbbsjuLL7QeBEJ/ZOHRA KbOcombsjNusE0fEsPASwMddJTtrQh5C8LJzlCu20XhmE06ARb2KJBBI2Le4roczHqL/NgbTy FX1kIOkyCvJZbrFaDrp5UK96Zj3zDtyIhRbAXbKk+wjIyoyl+cnoZ5l7et8YyHE90Iv4Luub5 Bu48qdELuJLDBJRPBbM7pzLcvi0KZifp+WfKGdQotOpnw/Q/EPbunQcrKXww1v9DTtOaZAvMr MXxJdxXPYvZRD9XNcjpT0CpSLHwJUZVmWAnQEFJYa2akTMV9IFG+n2RubPFYezTnumf61vdwO Kg2hiTPwnzWYY4BpmRNg== Received-SPF: pass client-ip=212.227.15.15; 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: Fri, 05 Feb 2021 09:03:04 -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-- [Patch v2 2/2]: no change =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