Skip to content

Commit 2adef70

Browse files
committed
Use same format for scratch and slot trailer
Prior to this change, the scratch image trailer had a different format from a slot image trailer. Specifically: 1. The scratch trailer only contained a single set of status entries (three bytes); the slot trailer contained `BOOT_STATUS_MAX_ENTRIES` sets of status entries. 2. The scratch trailer did not contain the `copy_done` field. This inconsistency required some extra conditional logic in the trailer handling code. It is simpler to just use the same trailer format everywhere. This commit removes this inconsistency. Now, the scratch trailer structure is identical to that of the slot trailer. Signed-off-by: Christopher Collins <[email protected]>
1 parent 4a5477a commit 2adef70

File tree

4 files changed

+21
-55
lines changed

4 files changed

+21
-55
lines changed

boot/bootutil/src/bootutil_misc.c

Lines changed: 16 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -120,30 +120,16 @@ boot_flag_decode(uint8_t flag)
120120
}
121121

122122
uint32_t
123-
boot_slots_trailer_sz(uint8_t min_write_sz)
123+
boot_trailer_sz(uint8_t min_write_sz)
124124
{
125125
return /* state for all sectors */
126126
BOOT_STATUS_MAX_ENTRIES * BOOT_STATUS_STATE_COUNT * min_write_sz +
127127
#ifdef MCUBOOT_ENC_IMAGES
128128
/* encryption keys */
129129
BOOT_ENC_KEY_SIZE * 2 +
130130
#endif
131-
/* copy_done + image_ok + swap_size */
132-
BOOT_MAX_ALIGN * 3 +
133-
BOOT_MAGIC_SZ;
134-
}
135-
136-
static uint32_t
137-
boot_scratch_trailer_sz(uint8_t min_write_sz)
138-
{
139-
/* state for one sector */
140-
return BOOT_STATUS_STATE_COUNT * min_write_sz +
141-
#ifdef MCUBOOT_ENC_IMAGES
142-
/* encryption keys */
143-
BOOT_ENC_KEY_SIZE * 2 +
144-
#endif
145-
/* image_ok + swap_size */
146-
BOOT_MAX_ALIGN * 2 +
131+
/* swap_type + copy_done + image_ok + swap_size */
132+
BOOT_MAX_ALIGN * 4 +
147133
BOOT_MAGIC_SZ;
148134
}
149135

@@ -176,11 +162,7 @@ boot_status_off(const struct flash_area *fap)
176162

177163
elem_sz = flash_area_align(fap);
178164

179-
if (fap->fa_id == FLASH_AREA_IMAGE_SCRATCH) {
180-
off_from_end = boot_scratch_trailer_sz(elem_sz);
181-
} else {
182-
off_from_end = boot_slots_trailer_sz(elem_sz);
183-
}
165+
off_from_end = boot_trailer_sz(elem_sz);
184166

185167
assert(off_from_end <= fap->fa_size);
186168
return fap->fa_size - off_from_end;
@@ -189,7 +171,6 @@ boot_status_off(const struct flash_area *fap)
189171
static uint32_t
190172
boot_copy_done_off(const struct flash_area *fap)
191173
{
192-
assert(fap->fa_id != FLASH_AREA_IMAGE_SCRATCH);
193174
assert(offsetof(struct image_trailer, copy_done) == 0);
194175
return fap->fa_size - BOOT_MAGIC_SZ - BOOT_MAX_ALIGN * 2;
195176
}
@@ -204,27 +185,14 @@ boot_image_ok_off(const struct flash_area *fap)
204185
static uint32_t
205186
boot_swap_size_off(const struct flash_area *fap)
206187
{
207-
/*
208-
* The "swap_size" field if located just before the trailer.
209-
* The scratch slot doesn't store "copy_done"...
210-
*/
211-
if (fap->fa_id == FLASH_AREA_IMAGE_SCRATCH) {
212-
return fap->fa_size - BOOT_MAGIC_SZ - BOOT_MAX_ALIGN * 2;
213-
}
214-
215-
return fap->fa_size - BOOT_MAGIC_SZ - BOOT_MAX_ALIGN * 3;
188+
return fap->fa_size - BOOT_MAGIC_SZ - BOOT_MAX_ALIGN * 4;
216189
}
217190

218191
#ifdef MCUBOOT_ENC_IMAGES
219192
static uint32_t
220193
boot_enc_key_off(const struct flash_area *fap, uint8_t slot)
221194
{
222-
if (fap->fa_id == FLASH_AREA_IMAGE_SCRATCH) {
223-
return fap->fa_size - BOOT_MAGIC_SZ - BOOT_MAX_ALIGN * 2 -
224-
((slot + 1) * BOOT_ENC_KEY_SIZE);
225-
}
226-
227-
return fap->fa_size - BOOT_MAGIC_SZ - BOOT_MAX_ALIGN * 3 -
195+
return fap->fa_size - BOOT_MAGIC_SZ - BOOT_MAX_ALIGN * 4 -
228196
((slot + 1) * BOOT_ENC_KEY_SIZE);
229197
}
230198
#endif
@@ -248,18 +216,16 @@ boot_read_swap_state(const struct flash_area *fap,
248216
state->magic = boot_magic_decode(magic);
249217
}
250218

251-
if (fap->fa_id != FLASH_AREA_IMAGE_SCRATCH) {
252-
off = boot_copy_done_off(fap);
253-
rc = flash_area_read_is_empty(fap, off, &state->copy_done,
254-
sizeof state->copy_done);
255-
if (rc < 0) {
256-
return BOOT_EFLASH;
257-
}
258-
if (rc == 1) {
259-
state->copy_done = BOOT_FLAG_UNSET;
260-
} else {
261-
state->copy_done = boot_flag_decode(state->copy_done);
262-
}
219+
off = boot_copy_done_off(fap);
220+
rc = flash_area_read_is_empty(fap, off, &state->copy_done,
221+
sizeof state->copy_done);
222+
if (rc < 0) {
223+
return BOOT_EFLASH;
224+
}
225+
if (rc == 1) {
226+
state->copy_done = BOOT_FLAG_UNSET;
227+
} else {
228+
state->copy_done = boot_flag_decode(state->copy_done);
263229
}
264230

265231
off = boot_image_ok_off(fap);

boot/bootutil/src/bootutil_priv.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ struct boot_loader_state {
180180
int bootutil_verify_sig(uint8_t *hash, uint32_t hlen, uint8_t *sig,
181181
size_t slen, uint8_t key_id);
182182

183-
uint32_t boot_slots_trailer_sz(uint8_t min_write_sz);
183+
uint32_t boot_trailer_sz(uint8_t min_write_sz);
184184
int boot_status_entries(const struct flash_area *fap);
185185
uint32_t boot_status_off(const struct flash_area *fap);
186186
int boot_read_swap_state(const struct flash_area *fap,

boot/bootutil/src/loader.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1026,7 +1026,7 @@ boot_erase_trailer_sectors(const struct flash_area *fap)
10261026

10271027
/* delete starting from last sector and moving to beginning */
10281028
sector = boot_img_num_sectors(&boot_data, slot) - 1;
1029-
trailer_sz = boot_slots_trailer_sz(BOOT_WRITE_SZ(&boot_data));
1029+
trailer_sz = boot_trailer_sz(BOOT_WRITE_SZ(&boot_data));
10301030
total_sz = 0;
10311031
do {
10321032
sz = boot_img_sector_size(&boot_data, slot, sector);
@@ -1072,7 +1072,7 @@ boot_swap_sectors(int idx, uint32_t sz, struct boot_status *bs)
10721072
img_off = boot_img_sector_off(&boot_data, BOOT_PRIMARY_SLOT, idx);
10731073

10741074
copy_sz = sz;
1075-
trailer_sz = boot_slots_trailer_sz(BOOT_WRITE_SZ(&boot_data));
1075+
trailer_sz = boot_trailer_sz(BOOT_WRITE_SZ(&boot_data));
10761076

10771077
/* sz in this function is always sized on a multiple of the sector size.
10781078
* The check against the start offset of the last sector

sim/mcuboot-sys/src/c.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ pub fn boot_go(multiflash: &mut SimMultiFlash, areadesc: &AreaDesc,
4141
}
4242

4343
pub fn boot_trailer_sz(align: u8) -> u32 {
44-
unsafe { raw::boot_slots_trailer_sz(align) }
44+
unsafe { raw::boot_trailer_sz(align) }
4545
}
4646

4747
pub fn boot_magic_sz() -> usize {
@@ -87,7 +87,7 @@ mod raw {
8787
pub static mut c_asserts: u8;
8888
pub static mut c_catch_asserts: u8;
8989

90-
pub fn boot_slots_trailer_sz(min_write_sz: u8) -> u32;
90+
pub fn boot_trailer_sz(min_write_sz: u8) -> u32;
9191

9292
pub static BOOT_MAGIC_SZ: u32;
9393
pub static BOOT_MAX_ALIGN: u32;

0 commit comments

Comments
 (0)