|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +import { readFile, writeFile, appendFile } from "node:fs/promises" |
| 4 | +import { join } from "node:path" |
| 5 | +import { fileURLToPath } from "node:url" |
| 6 | + |
| 7 | +import { $ } from "execa" |
| 8 | + |
| 9 | +async function remountReadWrite(path) { |
| 10 | + const { stdout } = await $`findmnt --json --target ${path}` |
| 11 | + const result = JSON.parse(stdout) |
| 12 | + const filesystem = result.filesystems[0] |
| 13 | + const read_only = filesystem.options.split(",").includes("ro") |
| 14 | + |
| 15 | + if (read_only) { |
| 16 | + await $`mount -o remount,rw ${filesystem.target}` |
| 17 | + } |
| 18 | + return [read_only, filesystem] |
| 19 | +} |
| 20 | + |
| 21 | +async function remountReadWriteOnce(path, fn) { |
| 22 | + const [was_read_only, filesystem] = await remountReadWrite(path) |
| 23 | + |
| 24 | + try { |
| 25 | + await fn() |
| 26 | + } finally { |
| 27 | + if (was_read_only) { |
| 28 | + await $`mount -o remount,ro ${filesystem.target}` |
| 29 | + } |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +const path = "/boot/firmware/" |
| 34 | + |
| 35 | +// Configure firmware |
| 36 | +// https://www.raspberrypi.com/documentation/computers/config_txt.html |
| 37 | +async function process_config() { |
| 38 | + const path_config = join(path, "config.txt") |
| 39 | + |
| 40 | + const content = await readFile(path_config, "utf8") |
| 41 | + const include = await readFile( |
| 42 | + fileURLToPath(import.meta.resolve("./config.ini")), |
| 43 | + "utf8", |
| 44 | + ) |
| 45 | + |
| 46 | + if (!content.trim().includes(include.trim())) { |
| 47 | + await appendFile(path_config, include) |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +async function process_cmdline(path) { |
| 52 | + // Disable the 4 Raspberry logo in the top left corner |
| 53 | + // more space for kernel and system logs |
| 54 | + const content = await readFile(path, "utf8") |
| 55 | + const args = content.trim().split(" ") |
| 56 | + if (!args.includes("logo.nologo")) { |
| 57 | + args.push("logo.nologo") |
| 58 | + } |
| 59 | + await writeFile(path, args.join(" ")) |
| 60 | +} |
| 61 | + |
| 62 | +if (import.meta.main) { |
| 63 | + await remountReadWriteOnce(path, async () => { |
| 64 | + await process_config() |
| 65 | + |
| 66 | + for (const cmdline of ["cmdline.txt", "cmdline-A.txt", "cmdline-B.txt"]) { |
| 67 | + try { |
| 68 | + await process_cmdline(join(path, "cmdline.txt")) |
| 69 | + } catch (err) { |
| 70 | + if (err.code !== "ENOENT") throw err |
| 71 | + } |
| 72 | + } |
| 73 | + }) |
| 74 | +} |
0 commit comments