Commit graph

103 commits

Author SHA1 Message Date
Yifan Hong
b4b2bfc115 Allow to build non-sparse super image.
- Rename SparseBuilder to ImageBuilder, WriteSplitSparseFiles
  to WriteSplitImageFiles, WriteToSparseFile to WriteToImageFile, etc.
- Add sparsify argument to ImageBuilder, WriteToImageFile, and
  WriteSplitImageFiles

This allows lpmake to write non-sparse super image directly. Virtual
devices needs non-sparse images to work on.

Test: lpmake without --sparse
Test: liblp_test
Bug: 120041578

Change-Id: I76ee09efd02df7caaa8abd37a55ae0bebf9cfa29
Merged-In: I76ee09efd02df7caaa8abd37a55ae0bebf9cfa29
2019-04-04 16:34:05 -07:00
Yifan Hong
6c09f79eb1 liblp: Replace open with GetControlFileOrOpen
am: 26328e80b1

Change-Id: I8e65cef4b99cedf924e1fc0c75a0edc6fe1cca03
2019-03-26 14:28:42 -07:00
Nicolas Geoffray
7f03e4eea1 Allow makefiles to be parsed in a trimmed down manifest.
Test: m
Change-Id: Id0e52eacc8e8b5fa75af2050a72be6a8bdadc984
Exempt-From-Owner-Approval: Unbreaks ART team
2019-03-25 09:14:33 +00:00
Hridya Valsaraju
4e3b6332ab Create a VTS test from liblp_test
Bug: 129002747
Test: vts-tradefed run commandAndExit vts-kernel --primary-abi-only --module VtsKernelLiblpTest

Change-Id: I6497428ec1ce93f4c1814c3185c490d3701f1140
2019-03-22 15:11:49 -07:00
Yifan Hong
26328e80b1 liblp: Replace open with GetControlFileOrOpen
... so that fds from init can be used.

Bug: 126233777
Test: unit tests

Change-Id: Ife652e61305ef4fb6a02edfa765a91b5959d1b4b
2019-03-22 14:26:25 -07:00
David Anderson
71fc3f358b liblp: Reclaim wasted space from unaligned partitions, v2.
When allocating a partition with a size that is unaligned (to the
optimal alignment), the remaining sectors are wasted since they are
never reallocated. This is because the free list is guaranteed to only
contain optimally-aligned regions. Unfortunately this means when a
partition is resized, we are wasting a small amount of space each time.
On a non-A/B device, this could wind up being significant.

For example, with an alignment of 512KiB, a 4KiB partition at offset 0
will waste 508KiB of space. The next extent to be allocated by any
partition will start at the next 512KiB.

To address this, we check if the last extent for a partition can be
extended to cover the difference between its last sector and the next
optimally aligned sector. We also verify that this region was not
allocated to any other partition, and does not appear in the free list,
to make sure we're not stealing space that will be used somewhere else.

Bug: 120434950
Test: liblp_test gtest
Change-Id: I88689889d44a4d2c51e659241918aaf2c064e049
2019-03-14 12:09:09 -07:00
David Anderson
b8e1711980 Revert "liblp: Reclaim wasted space from unaligned partitions."
When this patch landed, it was thought that over-allocating partitions
had no undesired side effects. However there are cases where it is now
causing problems. When flashing a GSI for VTS, the vbmeta is appended to
the end of the partition. If libfs_avb cannot find this vbmeta by
looking at the end of the partition, verification will fail. Similar use
cases with verifying other dynamic partitions are expected to be
problematic, so for now we revert to block-size alignment.

This reverts commit dccfdca1e1.

Bug: 125113249
Test: liblp_test gtest
Change-Id: Ibfd66734b63265e9fbb2ccf6b670c168c36190bc
2019-03-13 12:58:21 -07:00
David Anderson
88e36c1514 Fix double-accounting bug in liblp.
When flashing in fastbootd, we create a new MetadataBuilder using the
given super_empty.img and attempt to import the existing partition
table. This will fail if there is some incompatibility in the partition
layout or partition quotas.

This import code was accidentally double-accounting partitions when
determining if they could fit within the group quota, preventing
"fastboot flashall" once partitions reached a certain size.

Bug: 126930319
Test: liblp_test gtest
Change-Id: I89a69cba110b62719197c9a4885cfc5bcf8f009f
2019-03-01 16:17:27 -08:00
Tom Cherry
679a6bacc6 Remove extranous reading of fstab in BuilderTest::block_device_info
Previously information from the fstab was needed for this test, but
that's not longer the case, so skip reading the fstab altogether.

Test: build
Change-Id: I3989c62e19ae2d8606f2bc3a617f9cc3da0e5a6f
2019-01-28 11:04:18 -08:00
David Anderson
2e1c5ac5fd Don't error on block size mismatches.
liblp treats the term "block size" ambiguously when it compares the logical
hardware block size with the file system block size (which for all
intents and purposes must be 4K). This warning is thus spurious on
devices with say a 512 logical block size. However, liblp's block size
should at least be a multiple of this, so change the check accordingly.

Bug: 123317012
Test: liblp_test gtest
Change-Id: I0f41f6bae60a512ab8d313e487c28606daa661a6
2019-01-23 19:24:03 -08:00
Treehugger Robot
7909d650bd Merge "Fixed a few minor nits in liblp" 2019-01-20 07:55:47 +00:00
David Anderson
34ad8ca773 Merge "liblp: Add helpers for modifying groups." 2019-01-11 01:45:21 +00:00
Ramon Pantin
c02be81efc Fixed a few minor nits in liblp
Bug: 122616553
Test: built and ran liblp_test on Pixel 3 with Q weekly build

Added missing __attribute__((packed)) in two metadata structures.
Fixed error logging message when repairing primary metadata.
Few very minor additions related to metadata validation.
Fixed an off by one error in the validation of partition name length.

Change-Id: Ic777baf97871c786a209da7c32bc13c1360a8482
Signed-off-by: Ramon Pantin <pantin@google.com>
2019-01-10 13:36:59 -08:00
David Anderson
c6c6e66813 liblp: Add helpers for modifying groups.
These are needed for non-A/B OTAs.

Bug: 122473283
Test: liblp_test gtest
Change-Id: Ib30614f1691dbea0a56c5a98aadc84fc26d1e639
2019-01-10 11:49:15 -08:00
Yifan Hong
e7f492dc61 liblp: BLKROSET 0 prior to FlashPartitionTable
Test: OTA after cppreopt
Change-Id: I0c611588fac73fbd8d1502cb57481f3363e0cbe8
Fixes: 122487294
2019-01-09 16:20:33 -08:00
David Anderson
126e481972 liblp: Allow GSI metadata to bypass slot suffixing checks.
Bug: 121210348
Test: manual test
Change-Id: I40634c72b0255197a5c16e23553f4cf54bc32579
2019-01-03 18:19:43 -08:00
David Anderson
189727089e liblp: Add API for adding predetermined linear extents to partitions
Bug: 121210348
Test: manual test
Change-Id: Ie9af456d5193e962a3bcf0cc5dcd22e794d143f6
2019-01-03 18:18:45 -08:00
Tom Cherry
898b642d8a fs_mgr: remove -Wno-unused-variable from cflags
Don't know why this was ever specified, but seems reasonable to remove
it now that we're so close.

Test: build
Change-Id: Ia8d056cd1c9660b3c22531317098ace78e661d6a
2018-12-12 10:30:10 -08:00
David Anderson
dccfdca1e1 liblp: Reclaim wasted space from unaligned partitions.
When allocating a partition with a size that is unaligned (to the
optimal alignment), the remaining sectors are wasted since they are
never reallocated. This is because the free list is guaranteed to only
contain optimally-aligned regions. Unfortunately this means when a
partition is resized, we are wasting a small amount of space each time.
On a non-A/B device, this could wind up being significant.

For example, with an alignment of 512KiB, a 4KiB partition at offset 0
will waste 508KiB of space. The next extent to be allocated by any
partition will start at the next 512KiB.

To address this, we round up extents to the optimal alignment. This
means partitions may wind up slightly over-allocated, versus before,
where they would waste space by making it unavailable.

Bug: 120434950
Test: liblp_test gtest
Change-Id: I555209b301058555526cc4309f7049ae81cf877d
2018-12-10 12:53:52 -08:00
David Anderson
8acb5dc1c5 liblp: Allocate "b" extents in the second half of super.
When allocating "b" partitions on a non-retrofit A/B device, prioritize
regions occuring in the second half of the super partition. To make this
effective, the region covering the midpoint sector is split into two
additional regions.

This will allow OTAs to avoid unecessary fragmentation, since each
slot's partitions will be grouped together, leaving a large chunk of
contiguous space available when the OTA deletes the target slot. Since
updates are not allowed to consume more than half of the super
partition, this should guarantee one extent per partition. Note that, if
this restriction is not in place (for example, a developer flashes a
massive "system_b"), then an additional extent will be allocated due to
the region that was split.

Bug: 120433288
Test: liblp_test gtest
Change-Id: I1797e59e14c8b0d4d0e6855a1d984e8159b21df2
2018-12-03 16:58:34 -08:00
David Anderson
66a6d8877c Merge "fastbootd: Support two super partitions for retrofit devices." 2018-11-21 17:40:43 +00:00
David Anderson
8771a95e4c Merge "fastboot: Warn when flashing dynamic partitions in the bootloader." 2018-11-21 17:40:01 +00:00
David Anderson
6900fce913 Merge "liblp: Modify NewForUpdate to accomodate two super partitions." 2018-11-21 17:36:48 +00:00
David Anderson
c15618a2c6 liblp: Modify NewForUpdate to accomodate two super partitions.
This method was designed for a single-super model, and now needs to
change to accomodate two super partitions (system_a and system_b, for
retrofitting).

NewForUpdate is supposed to transition metadata from one block device
to the next for updates. For normal devices this is a no-op, since
metadata only exists on one partition (super). For retrofit devices,
metadata exists on system_a and system_b. This has two implications.

First, any references to the source slot must be rewritten. For example
"vendor_b" must become "vendor_a". However this is not true of partition
names. Partitions/extents are cleared in the updated metadata since they
no longer have any meaning (the block device list has been
rewritten). We also clear groups since they are re-added during OTA.

The reason we have to do this rewriting is that slot suffixes are
automatically applied in ReadMetadata. We do not have access to the
original unsuffixed metadata that was written by the initial OTA.
This was a conscious design decision, since it localizes retrofitting
idiosyncracies to just a few places (ReadMetadata, NewForUpdate, and
fastbootd), minimizing the number of external callers that have to
understand auto-slot-suffixing.

It would be arguably cleaner if retrofit metadata was always serialized
*without* slot suffixes, thereby making NewForUpdate a no-op. However
this would necessitate changes to the API elsewhere. The functions that
read partition names would have to take a slot suffix, and this would
further complicate MetadataBuilder and fastbootd. Another solution would
be to augment LpMetadata to retain unsuffixed information, but this is
probably not worthwhile given that retrofitting is intended to be
surgical, and will have a shorter lifespan than the non-retrofit case.

Bug: 116802789
Test: liblp_test gtest
Change-Id: I33596d92b38c47bc70bc0aa37ed04f6f0b9d4b6f
2018-11-21 17:36:28 +00:00
David Anderson
d25f1c3775 fastbootd: Support two super partitions for retrofit devices.
Retrofit devices will have two super partitions, spanning the A and B
slots separately. By design an OTA will never cause "A" or "B"
partitions to be assigned to the wrong super. However, the same is not
true of fastbootd, where it is possible to flash the inactive slot. We
do not want, for example, logical "system_a" flashing to super_b.

When interacting with partitions, fastbootd now extracts the slot suffix
from a GetSuperSlotSuffix() helper. On retrofit devices, if the partition
name has a slot, that slot will override FastbootDevice::GetCurrentSlot.
This forces partitions in the inactive slot to be assigned to the correct
super.

There are two consequences of this. First, partitions with no slot
suffix will default to the current slot. That means it is possible to
wind up with two "scratch" partitions, if "adb remount" is used on both
the "A" and "B" slots. However, only the active slot's "scratch" will be
visible to the user (either through adb or fastboot).

Second, if one slot does not have dynamic partitions, flashing will
default to fixed partitions. For example, if the A slot is logical and B
is not, flashing "system_a" will be logical and "system_b" will be
fixed. This works no matter which slot is active. We do not try to
upgrade the inactive slot to dynamic partitions.

Bug: 116802789
Test: fastboot set_active a
      fastboot flashall # dynamic partitions
      fastboot getvar is-logical:system_a # true
      fastboot getvar is-logical:system_b # false
      fastboot set_active b
      fastboot flashall --skip-secondary
      fastboot getvar is-logical:system_a # true
      fastboot getvar is-logical:system_b # true
      Booting both slots works.

Change-Id: Ib3c91944aaee1a96b2f5ad69c90e215bd6c5a2e8
2018-11-20 13:47:01 -08:00
David Anderson
8956964ee2 fastboot: Warn when flashing dynamic partitions in the bootloader.
On retrofit devices, it is easy to accidentally overwrite
system/vendor/product by flashing system in the bootloader. The reason
is that GPT system_a is really the super partition, and the bootloader
doesn't know it.

Addressing this in bootloaders would require two separate commands: one
that rejects flashing system/vendor/product, and another for
expert/factory use that would allow direct flashing.

This patch introduces protection into the host fastboot tool instead.
It's not mutually exclusive with bootloader changes; having protection
in the host tool affords us better and consistent UI. However it does
rely on users having newer builds.

With this change, the following will not work in the bootloader:

    fastboot flash system       # or vendor, product, etc

The message is the same whether or not the device is a retrofit. To
continue anyway, you can do:

    fastboot flash --force system

If we decide on bootloader protection as well, the --force flag can be
re-used.

Bug: 119689480
Test: fastboot flash system # disallowed in bootloader, allowed in fastbootd
      fastboot flash --force system # allowed in bootloader
Change-Id: I0861e3f28a15be925886d5c30c7ebd4b20c477cf
2018-11-20 12:10:16 -08:00
David Anderson
a3c47e63a6 liblp: Enable building on Windows.
Bug: 119689480
Test: builds when fastboot.exe uses liblp
Change-Id: I8ba2ad51d806c4650a0f35d41e4906b703d4661d
2018-11-19 16:20:51 -08:00
David Anderson
eb1213f170 Merge "liblp: Auto-suffix group names." 2018-11-19 22:37:33 +00:00
David Anderson
7256eaa1b4 liblp: Auto-suffix group names.
This is needed for update_engine to properly clean old partitions on
retrofit devices.

Bug: 119687874
Test: liblp_test gtest
Change-Id: Ida9483ad3c127e357f45789540ebbedc9d3d3883
2018-11-19 11:20:09 -08:00
Yifan Hong
dca3b3bb2c liblp: MetadataBuilder::HasBlockDevice
Query whether a block device is used in the metadata.

Test: pass
Bug: 118506262
Change-Id: I67196b68918ac232e735b10a850299f8653e4d3f
2018-11-15 14:45:41 -08:00
Treehugger Robot
5729cff611 Merge "liblp: partition_opener: fix log; more logs" 2018-11-15 02:01:34 +00:00
Yifan Hong
098cc974de liblp: partition_opener: fix log; more logs
Test: manual
Change-Id: I48777a479651dbd389e0a4420505342cb272b9f2
2018-11-14 22:45:20 +00:00
David Anderson
a14f111377 liblp: Use TMPDIR instead of P_tmpdir.
lpmake should be using the intermediates directory for temporary work
rather than /tmp.  Add ability to respect TMPDIR environment as
inherited from TemporaryFile.

Bug: 119313545
Test: manual test
Change-Id: I1a0317538875ee37fb4066602ff7a75e4658d74b
2018-11-14 12:52:23 -08:00
Treehugger Robot
af4a0e846e Merge "liblp: SparseBuilder: num_blocks aren't check correctly" 2018-11-14 06:57:46 +00:00
Yifan Hong
6ddf9683a1 liblp: fix error message
Test: pass
Change-Id: I5a969c3fbabd8f32ea785a6196225e655205245b
2018-11-13 13:58:27 -08:00
Yifan Hong
27a34d71c1 liblp: SparseBuilder: num_blocks aren't check correctly
Test: builds

Change-Id: Ic0fe481cd168217d764b7c69dfe9da124a277f82
2018-11-13 12:57:44 -08:00
David Anderson
140d053c02 liblp: Add a helper method for upgrading metadata on retrofit devices.
This adds a new MetadataBuilder constructor, NewForUpdate, that can be
used by update_engine to simplify upgrading metadata. It is safe to call
whether or not the device is a retrofit. If the metadata has block
devices assigned to a specific slot, and that slot matches the slot
suffix, it will ensure that an equivalent entry exists for the alternate
slot.

Thus, if the source slot is _a and the target slot is _b, and the
metadata has "system_a" as a block device but not "system_b", this will
automatically add "system_b" as a block device.

Bug: 116802789
Test: liblp_test gtest
Change-Id: Ie89d4dbf4c708b5705e658220227ebf33fcb1930
2018-11-09 16:03:40 -08:00
David Anderson
3d08636934 liblp: Fix io_tests.
These broke after recent changes to use IPartitionOpener in more places.
The io_tests must now give block device info to TestPartitionOpener.

Bug: N/A
Test: liblp_test gtest
Change-Id: I0a6505c7223e74507dc13184069fdc34bb6b81e4
2018-11-08 19:04:05 -08:00
David Anderson
7165154334 liblp: Split super.img for retrofit devices.
On retrofit devices, we need images that can be flashed in the
bootloader for the component "super"partitions. This change rewrites
SparseBuilder so that it generates one sparse image per block device,
and now handles partitions that span across multiple block devices.

A new API function has been added to write the set of sparse images to
an output folder.

Bug: 118887112
Test: manual test, flash split images
Change-Id: Iff56efdcb7bdfd5bc8dd7ff44e8234e091ac2346
2018-11-08 14:19:42 -08:00
David Anderson
76cde4173a liblp: Allow automatic slot suffixing of partition names.
On retrofit devices, an OTA package or super_empty.img won't know which
slot it applies to. This is not an issue on devices shipping with
dynamic partitions, since they ship on the "a" slot.

To work around this, partitions and block devices can be flagged as
"auto-slot-suffixed". This indicates that ReadMetadata should
automatically append a slot suffix before returning the metadata. This
flag is added by MetadataBuilder when requested, and will be enabled via
lpmake separately.

After ReadMetadata has applied slot suffixes, it takes care to remove
the slot-suffix flag. This prevents the suffix from being applied twice,
if for example the metadata is then imported into a MetadataBuilder.

Bug: 116802789
Test: liblp_test gtest
      retrofit device boots

Change-Id: Ic7de06d31253a8d5b8d15c0d936175ca2939f857
2018-11-08 09:51:03 -08:00
David Anderson
a97d6e36d5 Merge "fastboot: Query the name of the super partition." 2018-11-06 22:59:17 +00:00
Treehugger Robot
d016c75104 Merge "fastboot: Fix flashing both slots with dynamic partitions." 2018-11-06 22:48:45 +00:00
David Anderson
88fd7e60d0 Merge "liblp: Fix flashing metadata to multiple block devices." 2018-11-06 19:45:26 +00:00
David Anderson
90fe0a43ab fastboot: Query the name of the super partition.
This patch adds a new variable, "super-partition-name", to query the
name of the super partition (with a slot suffix if it has one). The
fastboot flashing tool has been updated to query this variable.

Since the super partition name can no longer be determined without
fastbootd, the presence of super_empty.img is used to test for
dynamic partition support rather than the presence of a super partition.

Bug: 116802789
Test: fastboot flashall on retrofit device
Change-Id: If830768eba6de7f31ac3183c64167fae973c77a4
2018-11-06 11:43:04 -08:00
David Anderson
96a9fd4063 fastboot: Fix flashing both slots with dynamic partitions.
When updating the super partition, attempt to preserve partitions from
the other slot. If any partition can't be preserved, fail and require a
wipe (-w) to proceed. This allows two bootable builds to be flashed to
both slots.

The preserve operation can fail if the metadata is not compatible with
the old partition layout. For example, if the partition references a
group that no longer exists, or a group changed its capacity, or the
metadata's block device list or list contents changed.

Bug: N/A
Test: liblp_test gtest
      fastboot flashall --skip-secondary

Change-Id: I53fdd29bc1f0ef132005a93d3cf1cdcd7f2fc05f
2018-11-06 11:43:03 -08:00
David Anderson
f89b1df1a5 init: Regenerate uevents for dependent super partition devices.
This patch adds another uevent-regeneration pass to the first stage
mount. When the super partition spans multiple block devices, we need
/dev/block/by-name symlinks to have been created before we begin mapping
dynamic partitions.

Bug: 116802789
Test: retrofit device boots
Change-Id: I00bb277e1d81385a457c5b4205a95d8fbe365bb2
2018-11-05 17:53:49 -08:00
David Anderson
7632f0c286 liblp: Fix flashing metadata to multiple block devices.
Although metadata is only stored on the first block device, we ideally
want to validate all the block devices before flashing. This patch does
that and in the process converts ValidateAndSerializeMetadata to use
IPartitionOpener.

Bug: 116802789
Test: manual test
      liblp_test gtest

Change-Id: I3f24cfc6f5607309dc3cded0423326c5ba293d26
2018-11-05 19:57:47 +00:00
David Anderson
ef9740c67a liblp: Allow the super partition to span multiple block devices.
This patch allows the block device table in LpMetadataHeader to contain
additional partitions. MetadataBuilder can now resize partitions such
that are allocated across block devices as needed, however, it attempts
to minimize this by grouping free regions by device.

Bug: 116802789
Test: liblp_test gtests
      device with super partition flashes and boots

Change-Id: I9cf74c8925faf154703eeba2a26546a152efcaa2
2018-10-24 19:08:37 -07:00
David Anderson
7a6c511a6d liblp: Add an abstraction layer for opening partitions.
This change introduces an IPartitionOpener abstraction. Methods that
interact with live metadata, such as ReadMetadata, UpdatePartitionTable,
and FlashPartitionTable, now require an IPartitionOpener object. Its
purpose is dependency injection: it will make these methods much easier
to test when the super partition spans multiple block devices.

All non-test consumers should be using PartitionOpener, and as such,
some helper methods have been added that automatically create one.

Bug: 116802789
Test: liblp_test gtest
      device with super partition boots
Change-Id: I76725a5830ef643c5007c152c00ccaad8085151f
2018-10-24 14:18:38 -07:00
David Anderson
9a5324178e liblp: Store device information in a new block device table.
This patch removes the alignment, block device size, and starting sector
fields from LpGeometry into a new LpMetadataBlockDevice struct. The
metadata now contains a table of these structs, and the table will have
exactly one entry representing the super partition.

This refactoring will make it easier to have logical partitions span
multiple physical partitions. When that happens, the table will be
allowed to have more than one entry, and the first entry of the table
will be considered the "root" of the super partition.

Bug: 116802789
Test: liblp_test gtest
      device with logical partitions flashes and boots
Change-Id: I97f23beac0363182cb6ae78ba2595860950afcf0
2018-10-24 14:18:35 -07:00