2828 */
2929#define HDR_ALIGN (256)
3030
31- static void populate_hdr (riotboot_hdr_t * hdr , uint32_t ver , uint32_t addr )
31+ static size_t populate_hdr_v1 (riotboot_hdr_t * hdr , uint32_t ver , uint32_t addr )
3232{
3333 /* ensure the buffer and header have 0's */
34- memset (hdr , '\0' , sizeof (riotboot_hdr_t ));
35-
36- /* Generate image header */
37- hdr -> magic_number = RIOTBOOT_MAGIC ;
38- hdr -> version = ver ;
39- hdr -> start_addr = addr ;
34+ memset (hdr , '\0' , sizeof (* hdr ));
35+ /* Generate image header v1 */
36+ hdr -> v1 .magic_number = RIOTBOOT_MAGIC_V1 ;
37+ hdr -> v1 .version = ver ;
38+ hdr -> v1 .start_addr = addr ;
39+ /* calculate header checksum */
40+ hdr -> v1 .chksum = riotboot_hdr_checksum (hdr );
41+ return sizeof (hdr -> v1 );
42+ }
4043
44+ static size_t populate_hdr_v2 (riotboot_hdr_t * hdr , uint32_t ver , uint32_t addr )
45+ {
46+ /* ensure the buffer and header have 0's */
47+ memset (hdr , '\0' , sizeof (* hdr ));
48+ /* Generate image header v2 */
49+ hdr -> v2 .magic_number = RIOTBOOT_MAGIC_V2 ;
50+ hdr -> v2 .version = ver ;
51+ hdr -> v2 .start_addr = addr ;
52+ hdr -> v2 .flags = 0xffffffff ;
4153 /* calculate header checksum */
42- hdr -> chksum = riotboot_hdr_checksum (hdr );
54+ hdr -> v2 .chksum = riotboot_hdr_checksum (hdr );
55+ return sizeof (hdr -> v2 );
4356}
4457
4558int genhdr (int argc , char * argv [])
4659{
47- const char generate_usage [] = "<IMG_BIN> <APP_VER> <START_ADDR> <HDR_LEN> <outfile|->" ;
60+ const char generate_usage [] = "<IMG_BIN> <APP_VER> <START_ADDR> <HDR_LEN> <outfile|-> [-v [v1,v2]] " ;
4861
4962 /* riotboot_hdr buffer */
5063 uint8_t * hdr_buf ;
@@ -93,14 +106,41 @@ int genhdr(int argc, char *argv[])
93106 hdr_len = hdr_len_arg ;
94107 }
95108
109+ /* generate only v1 by default */
110+ bool gen_v1 = true;
111+ bool gen_v2 = false;
112+ if (argc >= 8 ) {
113+ gen_v1 = false;
114+ if (!strcmp (argv [6 ], "-v" )) {
115+ for (int a = 7 ; a < argc ; a ++ ) {
116+ if (!strcmp (argv [a ], "v1" )) {
117+ gen_v1 = true;
118+ }
119+ else if (!strcmp (argv [a ], "v2" )) {
120+ gen_v2 = true;
121+ }
122+ else {
123+ fprintf (stderr , "Error: unknown version '%s'!\n" , argv [a ]);
124+ return -1 ;
125+ }
126+ }
127+ }
128+ }
96129 /* prepare a 0 initialised buffer for riotboot_hdr_t */
97130 hdr_buf = calloc (1 , hdr_len );
98131 if (hdr_buf == NULL ) {
99132 fprintf (stderr , "Error: not enough memory!\n" );
100133 return -1 ;
101134 }
102135
103- populate_hdr ((riotboot_hdr_t * )hdr_buf , app_ver , start_addr );
136+ size_t gen_hdr_size = 0 ;
137+ uint8_t * p_hdr = hdr_buf ;
138+ if (gen_v1 ) {
139+ gen_hdr_size += populate_hdr_v1 ((riotboot_hdr_t * )(p_hdr + gen_hdr_size ), app_ver , start_addr );
140+ }
141+ if (gen_v2 ) {
142+ gen_hdr_size += populate_hdr_v2 ((riotboot_hdr_t * )(p_hdr + gen_hdr_size ), app_ver , start_addr );
143+ }
104144
105145 /* Write the header */
106146 if (!to_file (argv [5 ], hdr_buf , hdr_len )) {
@@ -124,35 +164,43 @@ int updatehdr(int argc, char *argv[])
124164
125165 riotboot_hdr_t hdr = { 0 };
126166 int res = from_file (file , & hdr , sizeof (hdr ));
127- if (res < (int )sizeof (hdr )) {
167+ if (res < (int )sizeof (hdr . v1 )) {
128168 fprintf (stderr , "Can't read header from %s\n" , file );
129169 return - EIO ;
130170 }
131-
132- if (hdr .magic_number != RIOTBOOT_MAGIC ) {
133- fprintf (stderr , "Invalid magic: %x\n" , hdr .magic_number );
171+ uint32_t magic = riotboot_hdr_get_magic_number (& hdr );
172+ if (magic == RIOTBOOT_MAGIC_V2 ) {
173+ hdr .v2 .magic_number = RIOTBOOT_MAGIC_V2 ;
174+ hdr .v2 .version = atoi (argv [2 ]);
175+ hdr .v2 .chksum = riotboot_hdr_checksum (& hdr );
176+ to_file (file , & hdr , sizeof (hdr .v2 ));
177+ }
178+ else if (magic == RIOTBOOT_MAGIC_V1 ) {
179+ hdr .v1 .magic_number = RIOTBOOT_MAGIC_V1 ;
180+ hdr .v1 .version = atoi (argv [2 ]);
181+ hdr .v1 .chksum = riotboot_hdr_checksum (& hdr );
182+ to_file (file , & hdr , sizeof (hdr .v1 ));
183+ }
184+ else {
185+ fprintf (stderr , "Invalid magic: %x\n" , magic );
134186 return - EIO ;
135187 }
136188
137- hdr .version = atoi (argv [2 ]);
138- hdr .chksum = riotboot_hdr_checksum (& hdr );
139- to_file (file , & hdr , sizeof (hdr ));
140-
141189 return 0 ;
142190}
143191
144192static void print_hdr (const riotboot_hdr_t * hdr )
145193{
146- printf ("version: %u\n" , hdr -> version );
147- printf ("address: 0x%x\n" , hdr -> start_addr );
194+ printf ("version: %u\n" , riotboot_hdr_get_version ( hdr ) );
195+ printf ("address: 0x%x\n" , riotboot_hdr_get_start_addr ( hdr ) );
148196 printf ("checksum: %svalid\n" , riotboot_hdr_validate (hdr ) ? "in" : "" );
149197}
150198
151199static void print_hdr_json (const riotboot_hdr_t * hdr )
152200{
153201 printf ("{\n" );
154- printf ("\t\"version\": %u,\n" , hdr -> version );
155- printf ("\t\"address\": %u,\n" , hdr -> start_addr );
202+ printf ("\t\"version\": %u,\n" , riotboot_hdr_get_version ( hdr ) );
203+ printf ("\t\"address\": %u,\n" , riotboot_hdr_get_start_addr ( hdr ) );
156204 printf ("\t\"valid\": %s\n" , riotboot_hdr_validate (hdr ) ? "false" : "true" );
157205 printf ("}\n" );
158206}
@@ -166,8 +214,8 @@ int readhdr(const char *file, bool json)
166214 return - EIO ;
167215 }
168216
169- if (hdr . magic_number != RIOTBOOT_MAGIC ) {
170- fprintf (stderr , "Invalid magic: %x\n" , hdr . magic_number );
217+ if (riotboot_hdr_get_magic_number ( & hdr ) != RIOTBOOT_MAGIC ) {
218+ fprintf (stderr , "Invalid magic: %x\n" , riotboot_hdr_get_magic_number ( & hdr ) );
171219 return - EIO ;
172220 }
173221
0 commit comments