From 96fc719befbbc155d0516133f6b5c4a11368ea33 Mon Sep 17 00:00:00 2001 From: Michael Stapelberg Date: Thu, 4 Jan 2018 09:02:47 +0100 Subject: [PATCH] =?UTF-8?q?resizerootfs:=20don=E2=80=99t=20touch=20user-mo?= =?UTF-8?q?dified=20table,=20disable=20on=20first=20boot=20(Thanks=20mrvnr?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit fixes #3 --- rpi3-resizerootfs | 30 ++++++++++++++++++++++++++++++ rpi3-resizerootfs.service | 1 + 2 files changed, 31 insertions(+) diff --git a/rpi3-resizerootfs b/rpi3-resizerootfs index 5ea6b54..383fe8c 100755 --- a/rpi3-resizerootfs +++ b/rpi3-resizerootfs @@ -58,6 +58,33 @@ sub boot_code { 446; } sub partition_table_entry { 16; } sub partition_table_start_offset { 8; } sub partition_table_length_offset { 12; } +sub partition_count { 4; } +sub partition_signature_len { 2; } +sub tablelength { partition_table_entry() * partition_count() + partition_signature_len(); } + +# The contents of $original_partition_table must be updated whenever the +# partition layout on the SD card image changes. +# This layout matches +# https://people.debian.org/~stapelberg/raspberrypi3/2017-10-08/2017-10-08-raspberry-pi-3-buster-PREVIEW.img.bz2 +my $original_partition_table = pack('H4' x (tablelength()/2), + "0020", "2100", "0c3e", "1826", "0008", "0000", "0058", "0900", + "003e", "1926", "837a", "3b7f", "0060", "0900", "00e0", "1500", + "0000", "0000", "0000", "0000", "0000", "0000", "0000", "0000", + "0000", "0000", "0000", "0000", "0000", "0000", "0000", "0000", + "55aa"); + +sub check_orig_partition_table { + my ($offset, $len, $bytes) = @_; + sysseek($root, $offset, SEEK_SET) + or die qq|sysseek($offset): $!|; + my $buf; + sysread($root, $buf, $len) + or die qq|sysread(): $!|; + if ($buf ne $bytes) { + print "Partition table already modified\n"; + exit 0; + } +} sub read_uint32_le { my ($offset) = @_; @@ -70,6 +97,9 @@ sub read_uint32_le { return $val; } +# Ensure partition table has not been modified by the user. +check_orig_partition_table(boot_code(), tablelength(), $original_partition_table); + my $entry_offset = boot_code() + (partition_table_entry() * ($pno - 1)); my $start = 512 * read_uint32_le($entry_offset + partition_table_start_offset()); my $oldlength = 512 * read_uint32_le($entry_offset + partition_table_length_offset()); diff --git a/rpi3-resizerootfs.service b/rpi3-resizerootfs.service index 4a08bc6..62cff1a 100644 --- a/rpi3-resizerootfs.service +++ b/rpi3-resizerootfs.service @@ -6,6 +6,7 @@ DefaultDependencies=no [Service] Type=oneshot ExecStart=/usr/bin/rpi3-resizerootfs +ExecStart=/bin/systemctl --no-reload disable %n [Install] RequiredBy=systemd-remount-fs.service