[SeaBIOS] [PATCH] NVRAM bootorder

Timothy Kenno Handojo posted 1 patch 1 week, 1 day ago
Makefile | 2 +-
src/boot.c | 9 +++-
src/nvram.c | 144 ++++++++++++++++++++++++++++++++++++++++++++++++++++
src/nvram.h | 46 +++++++++++++++++
4 files changed, 199 insertions(+), 2 deletions(-)
create mode 100644 src/nvram.c
create mode 100644 src/nvram.h
[SeaBIOS] [PATCH] NVRAM bootorder
Posted by Timothy Kenno Handojo 1 week, 1 day ago
Revision from a previous patch. Sorry it took so long.

This change allows seabios to load bootorder not only from CBFS but
also from the NVRAM as well, making use of coreboot's cmos.layout.

Tested on QEMU Q35

From 3aee65ff1a9d5926ff18fa0770362e59e8b52257 Mon Sep 17 00:00:00 2001
From: Timothy Kenno Handojo <timkenhan@mailbox.org>
Date: Wed, 22 Jan 2025 14:40:18 +0700
Subject: [PATCH] boot: Allow setting bootorder from CMOS entry

The original bootorder from CBFS remains, but takes lower priority.

Signed-off-by: Timothy Kenno Handojo <timkenhan@mailbox.org>
---
Makefile | 2 +-
src/boot.c | 9 +++-
src/nvram.c | 144 ++++++++++++++++++++++++++++++++++++++++++++++++++++
src/nvram.h | 46 +++++++++++++++++
4 files changed, 199 insertions(+), 2 deletions(-)
create mode 100644 src/nvram.c
create mode 100644 src/nvram.h

diff --git a/Makefile b/Makefile
index d3341870..48f0b9ac 100644
--- a/Makefile
+++ b/Makefile
@@ -46,7 +46,7 @@ SRC32FLAT=$(SRCBOTH) post.c e820map.c malloc.c
romfile.c x86.c \
fw/mtrr.c fw/xen.c fw/acpi.c fw/mptable.c fw/pirtable.c \
fw/smbios.c fw/romfile_loader.c fw/dsdt_parser.c hw/virtio-ring.c \
hw/virtio-pci.c hw/virtio-mmio.c hw/virtio-blk.c hw/virtio-scsi.c \
- hw/tpm_drivers.c hw/nvme.c sha256.c sha512.c
+ hw/tpm_drivers.c hw/nvme.c sha256.c sha512.c nvram.c
SRC32SEG=string.c output.c pcibios.c apm.c stacks.c hw/pci.c hw/serialio.c
DIRS=src src/hw src/fw vgasrc
diff --git a/src/boot.c b/src/boot.c
index 5c37dafd..c5e102cf 100644
--- a/src/boot.c
+++ b/src/boot.c
@@ -21,6 +21,8 @@
#include "string.h" // memset
#include "util.h" // irqtimer_calc
#include "tcgbios.h" // tpm_*
+#include "nvram.h" // get_nvram_bootorder
+
/****************************************************************
* Helper search functions
@@ -250,8 +252,13 @@ loadBootOrder(void)
{
if (!CONFIG_BOOTORDER)
return;
+ char *f = NULL;
+
+ f = get_nvram_bootorder();
+
+ if (!f)
+ f = romfile_loadfile("bootorder", NULL);
- char *f = romfile_loadfile("bootorder", NULL);
if (!f)
return;
diff --git a/src/nvram.c b/src/nvram.c
new file mode 100644
index 00000000..145a062d
--- /dev/null
+++ b/src/nvram.c
@@ -0,0 +1,144 @@
+// Code to manipulate NVRAM configuration
+//
+// Copyright (C) 2024-2025 Timothy Kenno Handojo <timkenhan@mailbox.org>
+// Taken and adapted from coreboot's libpayload
+//
+// This file may be distributed under the terms of the GNU LGPLv3 license.
+
+/* NOTE:
+To avoid confusion, it is important to keep in mind that in this file,
+the term CMOS does NOT refer to the RTC, but instead the NVRAM as a whole.
+While this technically includes the RTC part, the main focus wouldn't be it,
+and instead be the configurable parts (i.e. BIOS settings).
+*/
+
+#include "nvram.h"
+#include "string.h" // memcmp, strlen
+#include "malloc.h" // malloc_low
+#include "x86.h" // inb, outb
+#include "output.h" // dprintf
+#include "util.h" // cb_header, find_cb_table, find_cb_subtable
+
+#define CB_TAG_CMOS_OPTION_TABLE 0x00c8
+#define CB_TAG_OPTION 0x00c9
+#define CB_TAG_OPTION_CHECKSUM 0x00cc
+
+
+u8 nvram_read(u8 addr){
+ u16 rtc_port = addr < 128 ? RTC_PORT_STANDARD : RTC_PORT_EXTENDED;
+
+ outb(addr, rtc_port);
+ return inb(rtc_port + 1);
+}
+
+void nvram_write(u8 val, u8 addr)
+{
+ u16 rtc_port = addr < 128 ? RTC_PORT_STANDARD : RTC_PORT_EXTENDED;
+
+ outb(addr, rtc_port);
+ outb(val, rtc_port + 1);
+}
+
+struct nvram_accessor {
+ u8 (*read)(u8 reg);
+ void (*write)(u8 val, u8 reg);
+};
+
+struct nvram_accessor *use_nvram = &(struct nvram_accessor) {
+ nvram_read,
+ nvram_write
+};
+
+static struct cb_cmos_entries *lookup_cmos_entry(struct
cb_cmos_option_table *option_table, const char *name)
+{
+ struct cb_cmos_entries *cmos_entry, *next;
+ int len = name ? strlen(name) : 0;
+
+ /* CMOS entries are located right after the option table */
+ cmos_entry = (struct cb_cmos_entries*)((unsigned char *)option_table
+ option_table->header_length);
+ while (cmos_entry) {
+ if (memcmp((const char*)cmos_entry->name, name, len) == 0)
+ return cmos_entry;
+ next = (struct cb_cmos_entries*)((unsigned char *)cmos_entry +
cmos_entry->size);
+ cmos_entry = (next->tag == CB_TAG_OPTION) ? next : NULL;
+ }
+
+ dprintf(1, "ERROR: No such CMOS option (%s)\n", name);
+ return NULL;
+}
+
+static int get_cmos_value(const struct nvram_accessor *nvram, u32
bitnum, u32 len, void *valptr)
+{
+ u8 *value = valptr;
+ int offs = 0;
+ u32 addr, bit;
+ u8 reg8;
+
+ /* Convert to byte borders */
+ addr=(bitnum / 8);
+ bit=(bitnum % 8);
+
+ /* Handle single byte or less */
+ if(len <= 8) {
+ reg8 = nvram->read(addr);
+ reg8 >>= bit;
+ value[0] = reg8 & ((1 << len) -1);
+ return 0;
+ }
+
+ /* When handling more than a byte, copy whole bytes */
+ while (len > 0) {
+ len -= 8;
+ value[offs++]=nvram->read(addr++);
+ }
+
+ return 0;
+}
+
+int options_checksum_valid(const struct nvram_accessor *nvram, struct
cb_cmos_checksum *option_checksum)
+{
+ int i;
+ int checksum_location = option_checksum->location / 8;
+ u16 checksum = 0, checksum_old;
+
+ for(i = option_checksum->range_start; i <= option_checksum->range_end; i++) {
+ checksum += nvram->read(i);
+ }
+
+ checksum_old = ((nvram->read(checksum_location)<<8) |
nvram->read(checksum_location+1));
+
+ return (checksum_old == checksum);
+}
+
+void *get_nvram_bootorder()
+{
+ const char *name = "bootorder";
+ struct cb_header *cbh = find_cb_table();
+
+ if (!cbh)
+ return NULL;
+ struct cb_cmos_option_table *option_table = find_cb_subtable(cbh,
CB_TAG_CMOS_OPTION_TABLE);
+ struct cb_cmos_checksum *option_checksum = find_cb_subtable(cbh,
CB_TAG_OPTION_CHECKSUM);
+ if (option_table == NULL) {
+ dprintf(1, "Could not find coreboot option table.\n");
+ return NULL;
+ }
+
+ struct cb_cmos_entries *cmos_entry = lookup_cmos_entry(option_table, name);
+
+ if (!cmos_entry)
+ return NULL;
+ int cmos_length = cmos_entry->length;
+
+ /* extra byte to ensure 0-terminated strings */
+ void *buf = malloc_low(cmos_length+1);
+ memset(buf, 0, cmos_length+1);
+
+ if(!options_checksum_valid(use_nvram, option_checksum)) {
+ dprintf(1, "Invalid option checksum.\n");
+ return NULL;
+ }
+ get_cmos_value(use_nvram, cmos_entry->bit, cmos_entry->length, buf);
+
+ return buf;
+}
diff --git a/src/nvram.h b/src/nvram.h
new file mode 100644
index 00000000..e57b9e1c
--- /dev/null
+++ b/src/nvram.h
@@ -0,0 +1,46 @@
+// Definitions for NVRAM configuration
+
+/* NOTE:
+To avoid confusion, it is important to keep in mind that in this file,
+the term CMOS does NOT refer to the RTC, but instead the NVRAM as a whole.
+While this technically includes the RTC part, the main focus wouldn't be it,
+and instead be the configurable parts (i.e. BIOS settings).
+*/
+
+#ifndef __NVRAM_H
+#define __NVRAM_H
+
+#include "types.h" // u32, u8
+
+#define RTC_PORT_STANDARD 0x70
+#define RTC_PORT_EXTENDED 0x72
+
+struct cb_cmos_option_table {
+ u32 tag;
+ u32 size;
+ u32 header_length;
+};
+
+#define CB_CMOS_MAX_NAME_LENGTH 32
+struct cb_cmos_entries {
+ u32 tag;
+ u32 size;
+ u32 bit;
+ u32 length;
+ u32 config;
+ u32 config_id;
+ u8 name[CB_CMOS_MAX_NAME_LENGTH];
+};
+
+struct cb_cmos_checksum {
+ u32 tag;
+ u32 size;
+ u32 range_start;
+ u32 range_end;
+ u32 location;
+ u32 type;
+};
+
+void *get_nvram_bootorder();
+
+#endif
\ No newline at end of file
-- 
2.39.5
_______________________________________________
SeaBIOS mailing list -- seabios@seabios.org
To unsubscribe send an email to seabios-leave@seabios.org
[SeaBIOS] Re: [PATCH] NVRAM bootorder
Posted by Gerd Hoffmann 1 day, 12 hours ago
  Hi,

> +/* NOTE:
> +To avoid confusion, it is important to keep in mind that in this file,
> +the term CMOS does NOT refer to the RTC, but instead the NVRAM as a whole.
> +While this technically includes the RTC part, the main focus wouldn't be it,
> +and instead be the configurable parts (i.e. BIOS settings).
> +*/

Hmm, this adds more confusion rather than avoids it.

> +u8 nvram_read(u8 addr){
> + u16 rtc_port = addr < 128 ? RTC_PORT_STANDARD : RTC_PORT_EXTENDED;
> +
> + outb(addr, rtc_port);
> + return inb(rtc_port + 1);
> +}

So, this clearly does access the cmos (with "cmos" meaning "the nv
storage of the rtc chip").

> +static struct cb_cmos_entries *lookup_cmos_entry(struct
> cb_cmos_option_table *option_table, const char *name)

This looks like it goes access structs in cbfs to figure where in the
cmos the config data you want read is located.

I have to agree with Kevin that storing config data in cmos is a bad
idea.  It's not reliable.  Also there isn't much space.  The
bootorder file is not designed to be space efficient and can be
relatively large.  There is a reason why UEFI goes store configuration
data (aka EFI variables) in flash instead.

take care,
  Gerd

_______________________________________________
SeaBIOS mailing list -- seabios@seabios.org
To unsubscribe send an email to seabios-leave@seabios.org