4extern uint32_t app_base;
7static int check_partition(
struct partition *p) {
8 if (p->magic != PART_MAGIC) {
9 printf(
"Partition at %p has invalid magic\r\n", p);
12 if (p->layout_version != 1) {
13 printf(
"Partition at %p has invalid layout_version %i\r\n", p, p->layout_version);
16 uint32_t crc = calc_crc32((
void *)p,
sizeof(
struct partition), 4);
19 printf(
"Partition at %p has invalid checksum. Stored=%i,Calc=%i\r\n", p, p->crc, crc);
22 printf(
"Partition at %p is valid (sequence %i)\r\n", p, p->part_seq);
27void partition_init() {
28 int i = check_partition((
void *)PARTITION_BASE);
33 i = check_partition((
void *)PARTITION_BASE + 1024);
37 printf(
"Creating partition...\r\n");
43 np->magic = PART_MAGIC;
44 np->layout_version = 1;
46 struct appdef *ap = &np->appdefs;
48 ap->size =
sizeof(
struct appdef);
49 ap->app_header = (uint32_t)&app_base;
56 np->crc = calc_crc32(buf, 1024, 4);
58 printf(
"Writing partition to flash...\r\n");
59 flash_write((
void *)&buf, (
void *)PARTITION_BASE, 1024);
61static struct partition *get_active_partition() {
62 struct partition *p1 = (
void *)PARTITION_BASE;
63 struct partition *p2 = (
void *)PARTITION_BASE + 1024;
64 int v1 = check_partition(p1);
65 int v2 = check_partition(p2);
67 if ((v1 != 0) && (v2 != 0)) {
82 if (p2->part_seq > p1->part_seq) {
88static struct partition *get_next_partition_to_write() {
89 void *x = get_active_partition();
92 return (
void *)PARTITION_BASE;
94 if (x == (
void *)PARTITION_BASE) {
95 return (
void *)PARTITION_BASE + 1024;
97 return (
void *)PARTITION_BASE;
101void partition_set_latest_app(
void *base) {
102 struct partition *pw = get_next_partition_to_write();
103 struct partition *pa = get_active_partition();
105 printf(
"Active partition: 0x%p\r\n", pa);
106 printf(
"Write partition: 0x%p\r\n", pw);
109 memset(buf, 0, 1024);
113 uint8_t *source = (
void *)pa;
114 for (i = 0; i < 1024; i++) {
120 np->magic = PART_MAGIC;
121 np->layout_version = 1;
123 struct appdef *ap = &np->appdefs;
125 ap->size =
sizeof(
struct appdef);
126 ap->app_header = (uint32_t)base;
133 np->crc = calc_crc32(buf, 1024, 4);
135 printf(
"Writing partition @%p to flash...\r\n", pw);
int flash_write(byte *b, void *adr, int size)
unlock, erase, write, lock