patches and low-level development discussion
 help / color / mirror / code / Atom feed
* [PATCH] scripts/make-gpt.sh: make GPT offset configurable
@ 2022-09-28 14:02 Ivan Nikolaenko
  2022-09-29 10:22 ` Alyssa Ross
  0 siblings, 1 reply; 2+ messages in thread
From: Ivan Nikolaenko @ 2022-09-28 14:02 UTC (permalink / raw)
  To: devel; +Cc: Ivan Nikolaenko

These changes can be used in an external configuration layer as
follows:
    overlays = [
      (self: super:
        {
            gptOffset = 9437184;
        })
    ];

Signed-off-by: Ivan Nikolaenko <ivan.nikolaenko@unikie.com>
---

This patch is not a solution but just a proposal about how things can be done.
There is at least one another way of doing this (having our own make-gpt.sh script),
but I thought that editing of the current one is better way.

Also I don't know should we also consider other images and rootfs be fixed this way.

 img/live/Makefile    |  3 ++-
 img/live/default.nix |  1 +
 scripts/make-gpt.sh  | 22 ++++++++++++++++++++--
 3 files changed, 23 insertions(+), 3 deletions(-)

diff --git a/img/live/Makefile b/img/live/Makefile
index 9815c88..5a29156 100644
--- a/img/live/Makefile
+++ b/img/live/Makefile
@@ -19,7 +19,8 @@ build/live.img: $(SCRIPTS)/format-uuid.sh $(SCRIPTS)/make-gpt.sh build/boot.fat
 	    build/boot.fat:c12a7328-f81f-11d2-ba4b-00a0c93ec93b \
 	    build/rootfs.verity.superblock:2c7357ed-ebd2-46d9-aec1-23d437ec2bf5:$$($(SCRIPTS)/format-uuid.sh "$$(dd if=build/rootfs.verity.roothash bs=32 skip=1 count=1 status=none)") \
 	    $(ROOT_FS):4f68bce3-e8cd-4db1-96e7-fbcaf984b709:$$($(SCRIPTS)/format-uuid.sh "$$(head -c 32 build/rootfs.verity.roothash)") \
-	    $(EXT_FS):9293e1ff-cee4-4658-88be-898ec863944f
+	    $(EXT_FS):9293e1ff-cee4-4658-88be-898ec863944f \
+	    --gptoffset=$(GPT_OFFSET)
 	mv $@.tmp $@
 
 build/spectrum.conf: build/rootfs.verity.roothash
diff --git a/img/live/default.nix b/img/live/default.nix
index 5461384..c529206 100644
--- a/img/live/default.nix
+++ b/img/live/default.nix
@@ -34,6 +34,7 @@ stdenvNoCC.mkDerivation {
   KERNEL = "${rootfs.kernel}/${stdenv.hostPlatform.linux-kernel.target}";
   ROOT_FS = rootfs;
   SYSTEMD_BOOT_EFI = "${systemd}/lib/systemd/boot/efi/systemd-bootx64.efi";
+  GPT_OFFSET=pkgs.gptOffset or null;
 
   buildFlags = [ "build/live.img" ];
   makeFlags = [ "SCRIPTS=${scripts}" ];
diff --git a/scripts/make-gpt.sh b/scripts/make-gpt.sh
index 56076d3..e8dd067 100755
--- a/scripts/make-gpt.sh
+++ b/scripts/make-gpt.sh
@@ -41,8 +41,25 @@ shift
 nl=$'\n'
 table="label: gpt"
 
-# Keep 1MiB free at the start, and 1MiB free at the end.
-gptBytes=$TWO_MiB
+TEMP=`getopt -o g:: --long gptoffset:: -- "$@"`
+eval set -- "$TEMP"
+
+while true ; do
+    case "$1" in
+        -g|--gptoffset)
+            gptBytes=$2 ; shift 2 ;;
+        --) shift ; break ;;
+        *) break ;;
+    esac
+done
+
+if [ -z "$gptBytes" ]
+then
+      gptBytes=${TWO_MiB}
+fi
+
+FIRST_LBA="$(expr ${gptBytes} / 1024)"
+
 for partition; do
 	sizeMiB="$(sizeMiB "$(partitionPath "$partition")")"
 	table="$table${nl}size=${sizeMiB}MiB,$(awk -f "$scriptsDir/sfdisk-field.awk" -v partition="$partition")"
@@ -52,6 +69,7 @@ done
 rm -f "$out"
 truncate -s "$gptBytes" "$out"
 sfdisk "$out" <<EOF
+first-lba: ${FIRST_LBA}
 $table
 EOF
 
-- 
2.25.1



^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] scripts/make-gpt.sh: make GPT offset configurable
  2022-09-28 14:02 [PATCH] scripts/make-gpt.sh: make GPT offset configurable Ivan Nikolaenko
@ 2022-09-29 10:22 ` Alyssa Ross
  0 siblings, 0 replies; 2+ messages in thread
From: Alyssa Ross @ 2022-09-29 10:22 UTC (permalink / raw)
  To: Ivan Nikolaenko; +Cc: devel, Ville Ilvonen

[-- Attachment #1: Type: text/plain, Size: 5138 bytes --]

Ivan Nikolaenko <ivan.nikolaenko@unikie.com> writes:

> These changes can be used in an external configuration layer as
> follows:
>     overlays = [
>       (self: super:
>         {
>             gptOffset = 9437184;
>         })
>     ];
>
> Signed-off-by: Ivan Nikolaenko <ivan.nikolaenko@unikie.com>
> ---
>
> This patch is not a solution but just a proposal about how things can be done.
> There is at least one another way of doing this (having our own make-gpt.sh script),
> but I thought that editing of the current one is better way.
>
> Also I don't know should we also consider other images and rootfs be fixed this way.
>
>  img/live/Makefile    |  3 ++-
>  img/live/default.nix |  1 +
>  scripts/make-gpt.sh  | 22 ++++++++++++++++++++--
>  3 files changed, 23 insertions(+), 3 deletions(-)

Hi Ivan, thanks for the proposal.

I still think this is a cross-distribution problem looking for a
cross-distribution solution, not something specific to Spectrum.  Other
distributions providing vendor-neutral ARM images (e.g. Fedora, NixOS)
have exactly the same problem.  Solving it as part of Spectrum doesn't
help them solve the same problem, and I think it's critically important
for the long term maintainability of Spectrum that we be wary of
implementing custom solutions for cross-distro problems, even when they
start out pretty small like this one.

So I think the best way forward here would be to have a small program
that lives outside of Spectrum.  It would take a firmware image, a
generic ARM OS GPT image, and a configurable offset (or, if more
board-specific knowledge ends up being required, some sort of board
configuration file).  It would then create a new GPT image, install the
firmware appropriately, and then copy the partitions from the generic
image, offset appropriately.

[[ To be honest, I'm surprised this program doesn't exist already
considering its limited scope, but I haven't seen anything like it.  I
suspect part of the reason is that most distros have still not caught up
with the idea of installing an image, rather than doing a bespoke
imperative installation, but that's definitely the direction things are
moving in — e.g. systemd is doing lots of work to support image-based
installs. [1] ]]

This approach would avoid introducing additional complexity into
Spectrum, _and_ would work for other operating systems too.  It could be
_the_ way to produce firmware+OS images for people who need to do that,
reducing the amount of effort required to be spent on this problem
across the ecosystem by users, integrators, and distributors.  And the
program should be pretty small and easy to write.

It wouldn't be duplicating Spectrum's make-gpt.sh particularly, because
the two would be focused on different tasks.  make-gpt.sh takes some
filesystem images and produces a GPT image from them, whereas this
program would take an existing GPT and add in the platform-specific
firmware.  And it wouldn't need to be duplicated for every board
somebody wanted to build images for, because the same program should be
flexible enough to work for all boards that work this way.

Some background here that's important to remember is that this is not
something most end users would be expected to need to do.  The ideal way
of installing Spectrum on ARM, for an end user, would be to install a
firmware distribution on separate storage (or if their board comes with
firmware pre-installed that supports standards-based boot, they don't
even have to do that!), and then install Spectrum on main storage.  The
firmware and OS could be updated and maintained independently*, and so
Spectrum doesn't have to also be an ARM firmware distribution — we can
leave that to other capable projects.  Combined firmware+OS images are
therefore mainly useful to two groups: end users using boards that don't
have separate storage for firmware (or for which there is no firmware
available that supports booting from main storage), or organisations who
want to roll a system out across lots of devices, for whom it's more
convenient to only have a single image on a single type of storage to
worry about.

If there's some reason this wouldn't work, and we absolutely *need*
changes in Spectrum to accomodate firmware in the images we build, I'm
willing to reconsider, but with my current knowledge, it looks to me
like it could be solved distribution-independently, outside of Spectrum,
and that doing so would make things better for everybody trying to
create vendor-neutral ARM OSes.

Previous discussion on this topic:

 - https://spectrum-os.org/lists/archives/spectrum-devel/20220828164957.p3743hvijjrkm66b@x220.qyliss.net/
 - https://spectrum-os.org/lists/archives/spectrum-devel/0ead7a33-4c9b-6eab-26df-5bbe02fc4649@unikie.com/

[[ As a side note, EBBR recommends using a protective partition rather than
just offseting the GPT, to make the firmware area of the image more
visible. [1] ]]

* I'd really like fwupd support in Spectrum.

[1]: https://arm-software.github.io/ebbr/index.html#gpt-partitioning

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 832 bytes --]

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-09-29 10:22 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-09-28 14:02 [PATCH] scripts/make-gpt.sh: make GPT offset configurable Ivan Nikolaenko
2022-09-29 10:22 ` Alyssa Ross

Code repositories for project(s) associated with this public inbox

	https://spectrum-os.org/git/crosvm
	https://spectrum-os.org/git/doc
	https://spectrum-os.org/git/mktuntap
	https://spectrum-os.org/git/nixpkgs
	https://spectrum-os.org/git/spectrum
	https://spectrum-os.org/git/ucspi-vsock
	https://spectrum-os.org/git/www

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).