Re: [PATCH 1/2] efi: SPI NOR flash support

2021-02-05 Thread Michael Lawnick

I introduced a bug by blindly copying header from unverified source.
Version 2 following

___
Grub-devel mailing list
Grub-devel@gnu.org
https://lists.gnu.org/mailman/listinfo/grub-devel


[PATCH v2 1/2] efi: SPI NOR flash support

2021-02-05 Thread Michael Lawnick

Add EFI SPI NOR driver

Use UEFI interface for accessing SPI NOR flashes.
If supported the implementation of UEFI boot software abstracts
away all those ugly H/W details like SPI controller or protocol.
Provided functions:
grub_efi_spi_nor_
init
erase
write
read
flash_size
flash_id
erase_block_size

This driver might be used for further abstraction to a common
(SPI) flash interface.

Signed-off-by: Michael Lawnick 
---
[Patch v2 1/2] : fix flaw in EFI header, wrong sequence of methods.
---
diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def
index 68b9e9f68..4d775e5f6 100644
--- a/grub-core/Makefile.core.def
+++ b/grub-core/Makefile.core.def
@@ -446,7 +446,7 @@ image = {
   i386_pc = boot/i386/pc/boot.S;

   cppflags = '-DHYBRID_BOOT=1';
-
+
   i386_pc_ldflags = '$(TARGET_IMG_LDFLAGS)';
   i386_pc_ldflags = '$(TARGET_IMG_BASE_LDOPT),0x7C00';

@@ -656,6 +656,12 @@ module = {
   enable = i386_multiboot;
 };

+module = {
+  name = efi_spi_nor;
+  common = bus/spi/efi_spi_nor.c;
+  enable = efi;
+};
+
 module = {
   name = nativedisk;
   common = commands/nativedisk.c;
diff --git a/grub-core/bus/spi/efi_spi_nor.c
b/grub-core/bus/spi/efi_spi_nor.c
new file mode 100644
index 0..0e073b436
--- /dev/null
+++ b/grub-core/bus/spi/efi_spi_nor.c
@@ -0,0 +1,298 @@
+/*  efi_spi_nor.c  - Give access to SPI NOR flash through UEFI interface.
+ *  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+");
+
+#define EFI_SPI_NOR_FLASH_PROTOCOL_GUID \
+   { 0xb57ec3fe, 0xf833, 0x4ba6, \
+   {0x85, 0x78, 0x2a, 0x7d, 0x6a, 0x87, 0x44, 0x4b} \
+   }
+
+#define EFI_FLASHID_LEN 3
+
+struct efi_spi_nor_flash_protocol {
+   struct spi_nor  *spi_peripheral;
+   grub_efi_uint32_t   flash_size;
+   grub_efi_uint8_tdevice_id[EFI_FLASHID_LEN];
+   grub_efi_uint32_t   erase_block_size;
+
+   grub_efi_status_t (* get_flash_id)(struct efi_spi_nor_flash_protocol
*this,
+grub_uint8_t *buffer);
+   grub_efi_status_t (* read_data)(struct efi_spi_nor_flash_protocol *this,
+ grub_uint32_t offset, grub_uint32_t 
len, grub_uint8_t *data);
+   grub_efi_status_t (* lf_read_data)(struct efi_spi_nor_flash_protocol
*this,
+grub_uint32_t offset, 
grub_uint32_t len, grub_uint8_t *data);
+   grub_efi_status_t (* read_status)(struct efi_spi_nor_flash_protocol 
*this,
+   grub_uint32_t num_bytes, 
grub_uint8_t *status);
+   grub_efi_status_t (* write_status)(struct efi_spi_nor_flash_protocol
*this,
+grub_uint32_t num_bytes, 
grub_uint8_t *status);
+   grub_efi_status_t (* write_data)(struct efi_spi_nor_flash_protocol 
*this,
+  grub_uint32_t offset, grub_uint32_t 
len, grub_uint8_t *data);
+   grub_efi_status_t (* erase_blocks)(struct efi_spi_nor_flash_protocol
*this,
+grub_uint32_t offset, 
grub_uint32_t blk_count);
+};
+
+/* grub_efi_spi_nor_init - initialize access to SPI NOR flash device
+ *
+ * Search pool of SPI NOR flash devices known to underlying EFI bootware.
+ * Use  and  to filter out devices.
+ *
+ * IN: flash_id - optional, pointer to max 3 bytes
(EFI_FLASHID_LEN) to match against
+ *SPI flash JEDEC ID, use NULL if no filtering.
+ * IN: num_id_bytes - number of bytes in flash_id. Maximum 3 bytes
+ *are used for comparison.
+ * IN: instance - number of device occurances to skip
+ *
+ * returns : pointer to flash device or NULL on failure
+ */
+void *
+grub_efi_spi_nor_init(grub_uint8_t *flash_id, grub_uint32_t
num_id_bytes, grub_uint32_t instance)
+{
+   grub_efi_guid_t efi_guid_spi_nor_flash_protocol =
EFI_SPI_NOR_FLASH_PROTOCOL_GUID;
+   grub_efi_status_t ret;
+   grub_efi_uintn_t num_handles;
+   grub_efi_handle_t *handl

[PATCH v2 2/2] efi: SPI NOR flash command line

2021-02-05 Thread 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 = {
 module = {
   name = efi_spi_nor;
   common = bus/spi/efi_spi_nor.c;
+  common = commands/efi/spinorcmd.c;
   enable = efi;
 };

diff --git a/grub-core/commands/efi/spinorcmd.c
b/grub-core/commands/efi/spinorcmd.c
new file mode 100644
index 0..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 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 = 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 = 1;
+   *id_idx = 0;
+   *cnt_idx = 0;
+   *id_cnt = 0;
+
+   while (opt_idx < argc) {
+   if (!grub_strcmp(args[opt_idx],"-id")) {
+   opt_idx++;
+   *id_idx = opt_idx;
+
+   while ((opt_idx < argc)
+   && (grub_strcmp(args[opt_idx], "-cnt"))
+   && (*id_cnt < 3)) {
+   opt_idx++;
+   *id_cnt = *id_cnt + 1;
+   }
+
+   if (*id_cnt == 0)
+   return 1;
+   } else if (!grub_strcmp(args[opt_idx],"-cnt")) {
+   if (argc > opt_idx + 1) {
+   *cnt_idx = opt_idx + 1;
+   opt_idx += 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 = 0;
+   grub_uint8_t *device_id = NULL, devId[3];
+   grub_uint32_t instance = 0;
+
+   if (argc < 1) {
+   grub_printf("Missing argument\n");
+  

Can't boot GNU Mach with efi

2021-02-05 Thread Andrea G. Monaco

Hello,


I'm trying to boot GNU Mach with efi, but it just says "Warning: no
console will be available to os". What's the problem? Maybe that I'm
booting a 32 bit image from a 64 bit grub in efi? Or that Mach expects
to start in text mode and grub can't do that in efi?


Warmly,

-- 
Andrea G. Monaco
Hacker, mathematician, lgbt+ activist
"Hope will never be silent!", H. Milk

___
Grub-devel mailing list
Grub-devel@gnu.org
https://lists.gnu.org/mailman/listinfo/grub-devel


Re: Can't boot GNU Mach with efi

2021-02-05 Thread Samuel Thibault
Andrea G. Monaco, le ven. 05 févr. 2021 21:57:03 +0100, a ecrit:
> Or that Mach expects to start in text mode and grub can't do that in
> efi?

Mach doesn't initialize text mode itself indeed.

Samue

___
Grub-devel mailing list
Grub-devel@gnu.org
https://lists.gnu.org/mailman/listinfo/grub-devel