Skip to content

Commit 61bbfef

Browse files
committed
Separated library functions for in-app use
1 parent ce98d46 commit 61bbfef

6 files changed

Lines changed: 143 additions & 117 deletions

File tree

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ OBJS:= \
2222
./src/crypto.o \
2323
./src/wolfboot.o \
2424
./src/image.o \
25+
./src/libwolfboot.o \
2526
./lib/wolfssl/wolfcrypt/src/sha256.o \
2627
./lib/wolfssl/wolfcrypt/src/hash.o \
2728
./lib/wolfssl/wolfcrypt/src/wolfmath.o \

include/image.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@
44
#include <target.h>
55
#include <wolfboot/wolfboot.h>
66

7-
#define IMG_STATE_NEW 0xFF
8-
#define IMG_STATE_UPDATING 0x70
9-
#define IMG_STATE_TESTING 0x10
10-
#define IMG_STATE_SUCCESS 0x00
117

128
#define SECT_FLAG_NEW 0x0F
139
#define SECT_FLAG_SWAPPING 0x07

include/wolfboot/wolfboot.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@
2121
#define PART_UPDATE 1
2222
#define PART_SWAP 2
2323

24+
#define IMG_STATE_NEW 0xFF
25+
#define IMG_STATE_UPDATING 0x70
26+
#define IMG_STATE_TESTING 0x10
27+
#define IMG_STATE_SUCCESS 0x00
28+
2429
void wolfBoot_erase_partition(uint8_t part);
2530
void wolfBoot_update_trigger(void);
2631
void wolfBoot_success(void);

src/image.c

Lines changed: 0 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -192,91 +192,6 @@ int wolfBoot_verify_authenticity(struct wolfBoot_image *img)
192192
return 0;
193193
}
194194

195-
static uint8_t *get_trailer(uint8_t part)
196-
{
197-
if (part == PART_BOOT)
198-
return (void *)(WOLFBOOT_PARTITION_BOOT_ADDRESS + WOLFBOOT_PARTITION_SIZE);
199-
else if (part == PART_UPDATE)
200-
return (void *)(WOLFBOOT_PARTITION_UPDATE_ADDRESS + WOLFBOOT_PARTITION_SIZE);
201-
else
202-
return NULL;
203-
}
204-
205-
int wolfBoot_set_partition_state(uint8_t part, uint8_t newst)
206-
{
207-
uint8_t *trailer_end = get_trailer(part);
208-
uint32_t *magic;
209-
uint8_t *state;
210-
uint32_t wolfboot_magic_trail = WOLFBOOT_MAGIC_TRAIL;
211-
if (!trailer_end)
212-
return -1;
213-
magic = (uint32_t *)(trailer_end - sizeof(uint32_t));
214-
if (*magic != WOLFBOOT_MAGIC_TRAIL)
215-
hal_flash_write((uint32_t)magic, (void *)&wolfboot_magic_trail, sizeof(uint32_t));
216-
state = (trailer_end - sizeof(uint32_t)) - 1;
217-
if (*state != newst)
218-
hal_flash_write((uint32_t)state, (void *)&newst, 1);
219-
return 0;
220-
}
221-
222-
int wolfBoot_set_sector_flag(uint8_t part, uint8_t sector, uint8_t newflag)
223-
{
224-
uint8_t *trailer_end = get_trailer(part);
225-
uint32_t *magic;
226-
uint8_t *flags;
227-
uint8_t fl_value;
228-
uint32_t wolfboot_magic_trail = WOLFBOOT_MAGIC_TRAIL;
229-
uint8_t pos = sector >> 1;
230-
if (!trailer_end)
231-
return -1;
232-
magic = (uint32_t *)(trailer_end - sizeof(uint32_t));
233-
if (*magic != WOLFBOOT_MAGIC_TRAIL)
234-
hal_flash_write((uint32_t)magic, (void *)&wolfboot_magic_trail, sizeof(uint32_t));
235-
flags = (trailer_end - sizeof(uint32_t)) - pos;
236-
if (sector == (pos << 1))
237-
fl_value = (*flags & 0xF0) | (newflag & 0x0F);
238-
else
239-
fl_value = ((newflag & 0x0F) << 4) | (*flags & 0x0F);
240-
if (fl_value != *flags)
241-
hal_flash_write((uint32_t)flags, &fl_value, 1);
242-
return 0;
243-
}
244-
245-
int wolfBoot_get_partition_state(uint8_t part, uint8_t *st)
246-
{
247-
uint8_t *trailer_end = get_trailer(part);
248-
uint32_t *magic;
249-
uint8_t *state;
250-
if (trailer_end)
251-
return -1;
252-
magic = (uint32_t *)(trailer_end - sizeof(uint32_t));
253-
if (*magic != WOLFBOOT_MAGIC_TRAIL)
254-
return -1;
255-
state = (trailer_end - sizeof(uint32_t)) - 1;
256-
*st = *state;
257-
return 0;
258-
}
259-
260-
int wolfBoot_get_sector_flag(uint8_t part, uint8_t sector, uint8_t *flag)
261-
{
262-
uint8_t *trailer_end = get_trailer(part);
263-
uint32_t *magic;
264-
uint8_t *flags;
265-
uint8_t fl_value;
266-
uint32_t wolfboot_magic_trail = WOLFBOOT_MAGIC_TRAIL;
267-
uint8_t pos = sector >> 1;
268-
if (!trailer_end)
269-
return -1;
270-
magic = (uint32_t *)(trailer_end - sizeof(uint32_t));
271-
if (*magic != WOLFBOOT_MAGIC_TRAIL)
272-
return -1;
273-
flags = (trailer_end - sizeof(uint32_t)) - pos;
274-
if (sector == (pos << 1))
275-
*flag = *flags & 0x0F;
276-
else
277-
*flag = (*flags & 0xF0) >> 4;
278-
return 0;
279-
}
280195

281196
int wolfBoot_copy(uint32_t src, uint32_t dst, uint32_t size)
282197
{
@@ -293,31 +208,3 @@ int wolfBoot_copy(uint32_t src, uint32_t dst, uint32_t size)
293208
}
294209
return pos;
295210
}
296-
297-
void wolfBoot_erase_partition(uint8_t part)
298-
{
299-
if (part == PART_BOOT)
300-
hal_flash_erase(WOLFBOOT_PARTITION_BOOT_ADDRESS, WOLFBOOT_PARTITION_SIZE);
301-
if (part == PART_UPDATE)
302-
hal_flash_erase(WOLFBOOT_PARTITION_UPDATE_ADDRESS, WOLFBOOT_PARTITION_SIZE);
303-
#ifdef WOLFBOOT_PARTITION_SWAP_ADDRESS
304-
if (part == PART_SWAP)
305-
hal_flash_erase(WOLFBOOT_PARTITION_SWAP_ADDRESS, WOLFBOOT_SECTOR_SIZE);
306-
#endif
307-
}
308-
309-
void wolfBoot_update_trigger(void)
310-
{
311-
uint8_t st = IMG_STATE_UPDATING;
312-
hal_flash_unlock();
313-
wolfBoot_set_partition_state(PART_UPDATE, st);
314-
hal_flash_lock();
315-
}
316-
317-
void wolfBoot_success(void)
318-
{
319-
uint8_t st = IMG_STATE_SUCCESS;
320-
hal_flash_unlock();
321-
wolfBoot_set_partition_state(PART_BOOT, st);
322-
hal_flash_lock();
323-
}

src/libwolfboot.c

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
/* libwolfboot.c
2+
*
3+
* Copyright (C) 2018 wolfSSL Inc.
4+
*
5+
* This file is part of wolfBoot.
6+
*
7+
* wolfBoot is free software; you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation; either version 2 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* wolfBoot is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program; if not, write to the Free Software
19+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
20+
*/
21+
#include <hal.h>
22+
#include <stdint.h>
23+
#include <inttypes.h>
24+
#include <wolfboot/wolfboot.h>
25+
26+
static uint8_t *get_trailer(uint8_t part)
27+
{
28+
if (part == PART_BOOT)
29+
return (void *)(WOLFBOOT_PARTITION_BOOT_ADDRESS + WOLFBOOT_PARTITION_SIZE);
30+
else if (part == PART_UPDATE)
31+
return (void *)(WOLFBOOT_PARTITION_UPDATE_ADDRESS + WOLFBOOT_PARTITION_SIZE);
32+
else
33+
return (void *)0;
34+
}
35+
36+
int wolfBoot_set_partition_state(uint8_t part, uint8_t newst)
37+
{
38+
uint8_t *trailer_end = get_trailer(part);
39+
uint32_t *magic;
40+
uint8_t *state;
41+
uint32_t wolfboot_magic_trail = WOLFBOOT_MAGIC_TRAIL;
42+
if (!trailer_end)
43+
return -1;
44+
magic = (uint32_t *)(trailer_end - sizeof(uint32_t));
45+
if (*magic != WOLFBOOT_MAGIC_TRAIL)
46+
hal_flash_write((uint32_t)magic, (void *)&wolfboot_magic_trail, sizeof(uint32_t));
47+
state = (trailer_end - sizeof(uint32_t)) - 1;
48+
if (*state != newst)
49+
hal_flash_write((uint32_t)state, (void *)&newst, 1);
50+
return 0;
51+
}
52+
53+
int wolfBoot_set_sector_flag(uint8_t part, uint8_t sector, uint8_t newflag)
54+
{
55+
uint8_t *trailer_end = get_trailer(part);
56+
uint32_t *magic;
57+
uint8_t *flags;
58+
uint8_t fl_value;
59+
uint32_t wolfboot_magic_trail = WOLFBOOT_MAGIC_TRAIL;
60+
uint8_t pos = sector >> 1;
61+
if (!trailer_end)
62+
return -1;
63+
magic = (uint32_t *)(trailer_end - sizeof(uint32_t));
64+
if (*magic != WOLFBOOT_MAGIC_TRAIL)
65+
hal_flash_write((uint32_t)magic, (void *)&wolfboot_magic_trail, sizeof(uint32_t));
66+
flags = (trailer_end - sizeof(uint32_t)) - pos;
67+
if (sector == (pos << 1))
68+
fl_value = (*flags & 0xF0) | (newflag & 0x0F);
69+
else
70+
fl_value = ((newflag & 0x0F) << 4) | (*flags & 0x0F);
71+
if (fl_value != *flags)
72+
hal_flash_write((uint32_t)flags, &fl_value, 1);
73+
return 0;
74+
}
75+
76+
int wolfBoot_get_partition_state(uint8_t part, uint8_t *st)
77+
{
78+
uint8_t *trailer_end = get_trailer(part);
79+
uint32_t *magic;
80+
uint8_t *state;
81+
if (trailer_end)
82+
return -1;
83+
magic = (uint32_t *)(trailer_end - sizeof(uint32_t));
84+
if (*magic != WOLFBOOT_MAGIC_TRAIL)
85+
return -1;
86+
state = (trailer_end - sizeof(uint32_t)) - 1;
87+
*st = *state;
88+
return 0;
89+
}
90+
91+
int wolfBoot_get_sector_flag(uint8_t part, uint8_t sector, uint8_t *flag)
92+
{
93+
uint8_t *trailer_end = get_trailer(part);
94+
uint32_t *magic;
95+
uint8_t *flags;
96+
uint8_t fl_value;
97+
uint32_t wolfboot_magic_trail = WOLFBOOT_MAGIC_TRAIL;
98+
uint8_t pos = sector >> 1;
99+
if (!trailer_end)
100+
return -1;
101+
magic = (uint32_t *)(trailer_end - sizeof(uint32_t));
102+
if (*magic != WOLFBOOT_MAGIC_TRAIL)
103+
return -1;
104+
flags = (trailer_end - sizeof(uint32_t)) - pos;
105+
if (sector == (pos << 1))
106+
*flag = *flags & 0x0F;
107+
else
108+
*flag = (*flags & 0xF0) >> 4;
109+
return 0;
110+
}
111+
112+
void wolfBoot_erase_partition(uint8_t part)
113+
{
114+
if (part == PART_BOOT)
115+
hal_flash_erase(WOLFBOOT_PARTITION_BOOT_ADDRESS, WOLFBOOT_PARTITION_SIZE);
116+
if (part == PART_UPDATE)
117+
hal_flash_erase(WOLFBOOT_PARTITION_UPDATE_ADDRESS, WOLFBOOT_PARTITION_SIZE);
118+
if (part == PART_SWAP)
119+
hal_flash_erase(WOLFBOOT_PARTITION_SWAP_ADDRESS, WOLFBOOT_SECTOR_SIZE);
120+
}
121+
122+
void wolfBoot_update_trigger(void)
123+
{
124+
uint8_t st = IMG_STATE_UPDATING;
125+
hal_flash_unlock();
126+
wolfBoot_set_partition_state(PART_UPDATE, st);
127+
hal_flash_lock();
128+
}
129+
130+
void wolfBoot_success(void)
131+
{
132+
uint8_t st = IMG_STATE_SUCCESS;
133+
hal_flash_unlock();
134+
wolfBoot_set_partition_state(PART_BOOT, st);
135+
hal_flash_lock();
136+
}

src/wolfboot.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
*/
2121
#include <loader.h>
2222
#include <stdint.h>
23+
#include <wolfboot/wolfboot.h>
2324
extern unsigned int _stored_data;
2425
extern unsigned int _start_data;
2526
extern unsigned int _end_data;

0 commit comments

Comments
 (0)