diff options
Diffstat (limited to 'util')
| -rwxr-xr-x | util/grubpo/grubpo.sh | 68 | ||||
| -rw-r--r-- | util/nvmutil/nvmutil.c | 149 | ||||
| -rw-r--r-- | util/spkmodem_recv/spkmodem-recv.c | 43 |
3 files changed, 153 insertions, 107 deletions
diff --git a/util/grubpo/grubpo.sh b/util/grubpo/grubpo.sh new file mode 100755 index 00000000..87abc735 --- /dev/null +++ b/util/grubpo/grubpo.sh @@ -0,0 +1,68 @@ +#!/bin/sh + +# SPDX-License-Identifier: MIT + +# Copyright (c) 2026 Leah Rowe + +set -u -e + +urlmain="https://www.mirrorservice.org/sites/libreboot.org/release/misc/grub" +urlbkup="https://mirror.math.princeton.edu/pub/libreboot/misc/grub" + +# script to grab GNU gettext po files from translationproject.org - +# i noticed that the grub bootstrap script grabs these at build time, +# without actually checking signatures, and they could change on the +# server upstream at any time + +# this means that the GRUB build process is currently non-deterministic, +# which is a violation of libreboot policy. + +tmpdir="`mktemp -d`" +tmpmod="`mktemp -d`" + +mkdir -p "$tmpdir" "$tmpmod" || exit 1 + +( +cd "$tmpdir" || exit 1 +wget --mirror --level=1 -nd -nv -A.po -P 'po/.reference' \ + https://translationproject.org/latest/grub/ || \ + exit 1 +find -type f > "$tmpmod/tmplist" || exit 1 +while read -r f; do + printf "%s\n" "${f#./}" >> "$tmpmod/module.list" + + # now make the actual config files, but don't use + # the main upstream, because those files can change + # at any time. we will, over time, manually update + # our mirrors + + pkgname="${f##*/}" + [ -z "$pkgname" ] && printf "ERR\n" && exit 1 + + pkgsum="`sha512sum "$f" | awk '{print $1}'`" + + mkdir -p "$tmpmod/$pkgname" || exit 1 + + printf "# SPDX-License-Identifier: GPL-3.0-or-later\n\n" >> \ + "$tmpmod/$pkgname/module.cfg" || exit 1 + + printf "subcurl=\"%s/%s\"\n" "$urlmain" "$pkgname" >> \ + "$tmpmod/$pkgname/module.cfg" || exit 1 + printf "subcurl_bkup=\"%s/%s\"\n" "$urlbkup" "$pkgname" >> \ + "$tmpmod/$pkgname/module.cfg" || exit 1 + printf "subhash=\"%s\"\n" "$pkgsum" >> "$tmpmod/$pkgname/module.cfg" + +done < "$tmpmod/tmplist" || exit 1; : +mv "$tmpmod/tmplist" "$tmpdir" || exit 1 +) + +printf "tmpdir for modules: '%s'\n" "$tmpmod" + +rm -f "module.list" || exit 1 + +printf "Check directory for lbmk files: '%s'\n" "$tmpmod" +printf "This directory has the PO files: '%s'\n" "$tmpdir" + +exit 0 + + diff --git a/util/nvmutil/nvmutil.c b/util/nvmutil/nvmutil.c index 05459bb7..54a79c9e 100644 --- a/util/nvmutil/nvmutil.c +++ b/util/nvmutil/nvmutil.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: MIT */ -/* Copyright (c) 2022-2025 Leah Rowe <leah@libreboot.org> */ +/* Copyright (c) 2022-2026 Leah Rowe <leah@libreboot.org> */ /* Copyright (c) 2023 Riku Viitanen <riku.viitanen@protonmail.com> */ #include <sys/stat.h> @@ -15,10 +15,11 @@ #include <unistd.h> void cmd_setchecksum(void), cmd_brick(void), swap(int partnum), writeGbe(void), - cmd_dump(void), cmd_setmac(void), readGbe(void), checkdir(const char *path), - macf(int partnum), hexdump(int partnum), openFiles(const char *path), - cmd_copy(void), parseMacString(const char *strMac, uint16_t *mac), - cmd_swap(void); + cmd_dump(void), cmd_setmac(void), nvmalloc(void), readGbe(void), + checkdir(const char *path), macf(int partnum), hexdump(int partnum), + parseMacString(const char *strMac, uint16_t *mac), cmd_swap(void), + openFiles(const char *path), cmd_copy(void), writeGbe_part(int), + readGbe_part(int); int goodChecksum(int partnum); uint8_t hextonum(char chs), rhex(void); @@ -38,9 +39,9 @@ uint16_t mac[3] = {0, 0, 0}; ssize_t nf; size_t partsize, gbe[2]; uint8_t nvmPartChanged[2] = {0, 0}, do_read[2] = {1, 1}; -int flags, rfd, fd, part; +int flags, rfd, fd, part, e = 1; -const char *strMac = NULL, *strRMac = "xx:xx:xx:xx:xx:xx", *filename = NULL; +const char *strMac = NULL, *strRMac = "xx:xx:xx:xx:xx:xx", *fname = NULL; typedef struct op { char *str; @@ -57,11 +58,13 @@ op_t op[] = { }; void (*cmd)(void) = NULL; -#define ERR() errno = errno ? errno : ECANCELED -#define err_if(x) if (x) err(ERR(), "%s", filename) +#define SET_ERR(x) errno = errno ? errno : x +#define err_if(x) if (x) err(SET_ERR(ECANCELED), "%s", fname) -#define xopen(f,l,p) if ((f = open(l, p)) == -1) err(ERR(), "%s", l); \ - if (fstat(f, &st) == -1) err(ERR(), "%s", l) +#define xopen(f,l,p) \ + if ((f = open(l, p)) == -1) \ + err(SET_ERR(ECANCELED), "%s", l); \ + if (fstat(f, &st) == -1) err(SET_ERR(ECANCELED), "%s", l) #define word(pos16, partnum) ((uint16_t *) gbe[partnum])[pos16] #define setWord(pos16, p, val16) if (word(pos16, p) != val16) \ @@ -88,10 +91,10 @@ main(int argc, char *argv[]) fprintf(stderr, " %s FILE copy 0|1\n", argv[0]); fprintf(stderr, " %s FILE brick 0|1\n", argv[0]); fprintf(stderr, " %s FILE setchecksum 0|1\n", argv[0]); - err(errno = ECANCELED, "Too few arguments"); + err(SET_ERR(ECANCELED), "Too few arguments"); } - filename = argv[1]; + fname = argv[1]; flags = O_RDWR; @@ -105,23 +108,23 @@ main(int argc, char *argv[]) } checkdir("/dev/urandom"); - checkdir(filename); + checkdir(fname); #ifdef __OpenBSD__ err_if(unveil("/dev/urandom", "r") == -1); if (flags == O_RDONLY) { - err_if(unveil(filename, "r") == -1); + err_if(unveil(fname, "r") == -1); err_if(unveil(NULL, NULL) == -1); err_if(pledge("stdio rpath", NULL) == -1); } else { - err_if(unveil(filename, "rw") == -1); + err_if(unveil(fname, "rw") == -1); err_if(unveil(NULL, NULL) == -1); err_if(pledge("stdio rpath wpath", NULL) == -1); } #endif - openFiles(filename); + openFiles(fname); #ifdef __OpenBSD__ err_if(pledge("stdio", NULL) == -1); #endif @@ -134,7 +137,7 @@ main(int argc, char *argv[]) cmd = op[i].cmd; break; } - err(errno = EINVAL, "Too few args on command '%s'", + err(SET_ERR(EINVAL), "Too few args on command '%s'", op[i].str); } } else { @@ -154,6 +157,7 @@ main(int argc, char *argv[]) } err_if((errno = (cmd == NULL) ? EINVAL : errno)); + nvmalloc(); readGbe(); (*cmd)(); writeGbe(); @@ -166,7 +170,7 @@ void checkdir(const char *path) { if (opendir(path) != NULL) - err(errno = EISDIR, "%s", path); + err(SET_ERR(EISDIR), "%s", path); if (errno == ENOTDIR) errno = 0; err_if(errno); @@ -186,7 +190,7 @@ openFiles(const char *path) partsize = st.st_size >> 1; break; default: - err(errno = ECANCELED, "Invalid file size (not 8/16/128KiB)"); + err(SET_ERR(ECANCELED), "Invalid file size (not 8/16/128KiB)"); break; } @@ -194,41 +198,43 @@ openFiles(const char *path) } void -readGbe(void) +nvmalloc(void) { + /* same operations need the full block, others only 128 bytes */ + nf = NVM_SIZE; if ((cmd == cmd_swap) || (cmd == cmd_copy)) nf = SIZE_4KB; - else - nf = NVM_SIZE; + /* only read the part specified, for copy/setchecksum/brick */ if ((cmd == cmd_copy) || (cmd == cmd_setchecksum) || (cmd == cmd_brick)) do_read[part ^ 1] = 0; + /* only allocate one block if only one part being read */ char *buf = malloc(nf << (do_read[0] & do_read[1])); if (buf == NULL) err(errno, NULL); gbe[0] = (size_t) buf; - gbe[1] = gbe[0] + (nf * (do_read[0] & do_read[1])); - ssize_t tnr = 0; - - for (int p = 0; p < 2; p++) { - if (!do_read[p]) - continue; - - ssize_t nr = pread(fd, (uint8_t *) gbe[p], nf, p * partsize); - err_if(nr == -1); - if (nr != nf) - err(errno == ECANCELED, - "%ld bytes read from '%s', expected %ld bytes\n", - nr, filename, nf); + /* speedhack: for cmd copy, both pointers are set the same */ + gbe[1] = gbe[0] + (nf * (do_read[0] & do_read[1])); +} - tnr += nr; - swap(p); /* handle big-endian host CPU */ - } +void +readGbe(void) +{ + for (int p = 0; p < 2; p++) + if (do_read[p]) + readGbe_part(p); +} - printf("%ld bytes read from file '%s'\n", tnr, filename); +void +readGbe_part(int p) +{ + if (pread(fd, (uint8_t *) gbe[p], nf, p * partsize) != nf) + err(SET_ERR(ECANCELED), + "Can't read %ld b from '%s' p%d", nf, fname, p); + swap(p); /* handle big-endian host CPU */ } void @@ -262,12 +268,12 @@ parseMacString(const char *strMac, uint16_t *mac) { uint64_t total = 0; if (strnlen(strMac, 20) != 17) - err(errno = EINVAL, "Invalid MAC address string length"); + err(SET_ERR(EINVAL), "Invalid MAC address string length"); for (uint8_t h, i = 0; i < 16; i += 3) { if (i != 15) if (strMac[i + 2] != ':') - err(errno = EINVAL, + err(SET_ERR(EINVAL), "Invalid MAC address separator '%c'", strMac[i + 2]); @@ -275,7 +281,7 @@ parseMacString(const char *strMac, uint16_t *mac) for (int nib = 0; nib < 2; nib++, total += h) { if ((h = hextonum(strMac[i + nib])) > 15) - err(errno = EINVAL, "Invalid character '%c'", + err(SET_ERR(EINVAL), "Invalid character '%c'", strMac[i + nib]); /* If random, ensure that local/unicast bits are set */ @@ -291,9 +297,9 @@ parseMacString(const char *strMac, uint16_t *mac) } if (total == 0) - err(errno = EINVAL, "Invalid MAC (all-zero MAC address)"); + err(SET_ERR(EINVAL), "Invalid MAC (all-zero MAC address)"); if (mac[0] & 1) - err(errno = EINVAL, "Invalid MAC (multicast bit set)"); + err(SET_ERR(EINVAL), "Invalid MAC (multicast bit set)"); } uint8_t @@ -307,8 +313,7 @@ hextonum(char ch) return ch - 'a' + 10; else if ((ch == '?') || (ch == 'x') || (ch == 'X')) return rhex(); /* random hex value */ - else - return 16; /* error: invalid character */ + return 16; /* error: invalid character */ } uint8_t @@ -415,56 +420,36 @@ goodChecksum(int partnum) return 1; fprintf(stderr, "WARNING: BAD checksum in part %d\n", partnum); - errno = ECANCELED; + SET_ERR(ECANCELED); return 0; } void writeGbe(void) { - ssize_t tnw = 0; - - for (int p = 0; p < 2; p++) { - if ((!nvmPartChanged[p]) || (flags == O_RDONLY)) - continue; - - swap(p); /* swap bytes on big-endian host CPUs */ - ssize_t nw = pwrite(fd, (uint8_t *) gbe[p], nf, p * partsize); - err_if(nw == -1); - if (nw != nf) - err(errno == ECANCELED, - "%ld bytes written to '%s', expected %ld bytes\n", - nw, filename, nf); - - tnw += nf; - } - - if ((flags != O_RDONLY) && (cmd != cmd_dump)) { - if (nvmPartChanged[0] || nvmPartChanged[1]) - printf("The following nvm words were written:\n"); - cmd_dump(); - } - - if ((!tnw) && (flags != O_RDONLY) && (!errno)) - fprintf(stderr, "No changes needed on file '%s'\n", filename); - else if (tnw) - printf("%ld bytes written to file '%s'\n", tnw, filename); + for (int p = 0; p < 2; p++) + if ((nvmPartChanged[p]) && (flags != O_RDONLY)) + writeGbe_part(p); + err_if(close(fd) == -1); +} - if (tnw) - errno = 0; +void +writeGbe_part(int p) +{ + swap(p); /* swap bytes on big-endian host CPUs */ + if(pwrite(fd, (uint8_t *) gbe[p], nf, p * partsize) != nf) + err(SET_ERR(ECANCELED), + "Can't write %ld b to '%s' p%d", nf, fname, p); - err_if(close(fd) == -1); } void swap(int partnum) { - size_t w, x; uint8_t *n = (uint8_t *) gbe[partnum]; - int e = 1; - for (w = NVM_SIZE * ((uint8_t *) &e)[0], x = 1; w < NVM_SIZE; - w += 2, x += 2) { + for (size_t w = NVM_SIZE * ((uint8_t *) &e)[0], x = 1; + w < NVM_SIZE; w += 2, x += 2) { n[w] ^= n[x]; n[x] ^= n[w]; n[w] ^= n[x]; diff --git a/util/spkmodem_recv/spkmodem-recv.c b/util/spkmodem_recv/spkmodem-recv.c index 4467282d..38ceb72a 100644 --- a/util/spkmodem_recv/spkmodem-recv.c +++ b/util/spkmodem_recv/spkmodem-recv.c @@ -5,7 +5,7 @@ /* Forked from coreboot's version, at util/spkmodem_recv/ in coreboot.git, * revision 5c2b5fcf2f9c9259938fd03cfa3ea06b36a007f0 as of 3 January 2022. * This version is heavily modified, re-written based on OpenBSD Kernel Source - * File Style Guide (KNF); this change is Copyright 2023 Leah Rowe. */ + * File Style Guide (KNF); this change is Copyright 2023,2026 Leah Rowe. */ #include <err.h> #include <errno.h> @@ -25,28 +25,24 @@ #define ERR() (errno = errno ? errno : ECANCELED) #define reset_char() ascii = 0, ascii_bit = 7 +#define err_if(x) if (x) err(ERR(), NULL) signed short frame[MAX_SAMPLES], pulse[MAX_SAMPLES]; int ringpos, debug, freq_data, freq_separator, sample_count, ascii_bit = 7; char ascii = 0; -void handle_audio(void); -void decode_pulse(void); int set_ascii_bit(void); -void print_char(void); -void print_stats(void); +void handle_audio(void), decode_pulse(void), print_char(void), + print_stats(void); int main(int argc, char *argv[]) { - int c; #ifdef __OpenBSD__ - if (pledge("stdio", NULL) == -1) - err(ERR(), "pledge"); + err_if (pledge("stdio", NULL) == -1); #endif - while ((c = getopt(argc, argv, "d")) != -1) - if (!(debug = (c == 'd'))) - err(errno = EINVAL, NULL); + for (int c; (c = getopt(argc, argv, "d")) != -1;) + err_if (!(debug = (c == 'd'))); setvbuf(stdout, NULL, _IONBF, 0); while (!feof(stdin)) handle_audio(); @@ -65,7 +61,6 @@ handle_audio(void) decode_pulse(); return; } - if (set_ascii_bit() < 0) print_char(); sample_count = 0; @@ -85,9 +80,8 @@ decode_pulse(void) if (ferror(stdin) != 0) err(ERR(), "Could not read from frame."); - if ((pulse[ringpos] = (abs(frame[ringpos]) > THRESHOLD) ? 1 : 0)) - ++freq_separator; - ++ringpos; + pulse[ringpos] = (abs(frame[ringpos]) > THRESHOLD) ? 1 : 0; + freq_separator += pulse[ringpos++]; ringpos %= MAX_SAMPLES; ++sample_count; } @@ -104,6 +98,15 @@ set_ascii_bit(void) } void +print_stats(void) +{ + long stdin_pos; + err_if ((stdin_pos = ftell(stdin)) == -1); + printf ("%d %d %d @%ld\n", freq_data, freq_separator, + FREQ_DATA_THRESHOLD, stdin_pos - sizeof(frame)); +} + +void print_char(void) { if (debug) @@ -112,13 +115,3 @@ print_char(void) printf("%c", ascii); reset_char(); } - -void -print_stats(void) -{ - long stdin_pos; - if ((stdin_pos = ftell(stdin)) == -1) - err(ERR(), NULL); - printf ("%d %d %d @%ld\n", freq_data, freq_separator, - FREQ_DATA_THRESHOLD, stdin_pos - sizeof(frame)); -} |
