33#include <stdint.h>
44#include <target.h>
55#include <wolfboot/wolfboot.h>
6+ #include "image.h"
67
78
89#define SECT_FLAG_NEW 0x0F
1112#define SECT_FLAG_UPDATED 0x00
1213
1314
14- #ifdef EXT_FLASH
15- # ifndef PART_UPDATE_DRIVER
16- # define PART_UPDATE_DRIVER (&internal_flash_driver)
17- # endif
18- # define flash_lock (x ) (x->flash_driver.lock())
19- # define flash_unlock (x ) (x->flash_driver.unlock())
20- # define flash_erase (x , addr , len ) (x->flash_driver.erase(addr, len)
21- # define flash_write (x , addr , data , len ) (x->flash_driver.write(addr, data, len)
22- # define flash_read (x , addr , data , len ) (x->flash_driver.read(addr, data, len)
23- #else
24- # define flash_lock (x ) hal_flash_lock()
25- # define flash_unlock (x ) hal_flash_unlock()
26- # define flash_erase (x , addr , len ) (x->flash_driver.erase(addr, len)
27- # define flash_write (x , addr , data , len ) (x->flash_driver.write(addr, data, len)
28- # define flash_read (x , addr , data , len ) (memcpy(data, addr, len) - data + len)
29- #endif
30-
31- struct wolfBoot_flash_driver {
32- int (* write )(uint32_t address , const uint8_t * data , int len );
33- int (* read )(uint32_t address , uint8_t * data , int len );
34- int (* erase )(uint32_t address , int len );
35- void (* lock )(void );
36- void (* unlock )(void );
37- };
38-
39- extern const struct wolfBoot_flash_driver internal_flash_driver ;
4015
4116struct wolfBoot_image {
4217 uint8_t * hdr ;
@@ -47,7 +22,6 @@ struct wolfBoot_image {
4722 uint8_t * fw_base ;
4823 uint32_t fw_size ;
4924 uint8_t part ;
50- struct wolfBoot_flash_driver * flash_driver ;
5125};
5226
5327
@@ -59,4 +33,64 @@ int wolfBoot_set_sector_flag(uint8_t part, uint8_t sector, uint8_t newflag);
5933int wolfBoot_get_partition_state (uint8_t part , uint8_t * st );
6034int wolfBoot_get_sector_flag (uint8_t part , uint8_t sector , uint8_t * flag );
6135
36+ #ifdef EXT_FLASH
37+ # ifdef PART_UPDATE_EXT
38+ # define UPDATE_EXT 1
39+ # else
40+ # define UPDATE_EXT 0
41+ # endif
42+ # ifdef PART_SWAP_EXT
43+ # define SWAP_EXT 1
44+ # else
45+ # define SWAP_EXT 0
46+ # endif
47+ # define PART_IS_EXT (x ) (((x)->part == PART_UPDATE)?UPDATE_EXT:(((x)->part == PART_SWAP)?SWAP_EXT:0))
48+ #include "hal.h"
49+
50+ static inline int wb_flash_erase (struct wolfBoot_image * img , uint32_t off , uint32_t size )
51+ {
52+ if (PART_IS_EXT (img ))
53+ return ext_flash_erase ((uint32_t )(img -> hdr ) + off , size );
54+ else
55+ return hal_flash_erase ((uint32_t )(img -> hdr ) + off , size );
56+ }
57+
58+ static inline int wb_flash_write (struct wolfBoot_image * img , uint32_t off , const void * data , uint32_t size )
59+ {
60+ if (PART_IS_EXT (img ))
61+ return ext_flash_write ((uint32_t )(img -> hdr ) + off , data , size );
62+ else
63+ return hal_flash_write ((uint32_t )(img -> hdr ) + off , data , size );
64+ }
65+
66+ static inline int wb_flash_write_verify_word (struct wolfBoot_image * img , uint32_t off , uint32_t word )
67+ {
68+ int ret ;
69+ volatile uint32_t copy ;
70+ if (PART_IS_EXT (img ))
71+ {
72+ ext_flash_read ((uint32_t )(img -> hdr ) + off , (void * )& copy , sizeof (uint32_t ));
73+ while (copy != word ) {
74+ ret = ext_flash_write ((uint32_t )(img -> hdr ) + off , (void * )& word , sizeof (uint32_t ));
75+ if (ret < 0 )
76+ return ret ;
77+ ext_flash_read ((uint32_t )(img -> hdr ) + off , (void * )& copy , sizeof (uint32_t ));
78+ }
79+ } else {
80+ volatile uint32_t * pcopy = (volatile uint32_t * )(img -> hdr + off );
81+ while (* pcopy != word ) {
82+ hal_flash_write ((uint32_t )pcopy , (void * )& word , sizeof (uint32_t ));
83+ }
84+ }
85+ return 0 ;
86+ }
87+
88+
89+ #else
90+ # define PART_IS_EXT (x ) (0)
91+ # define wb_flash_erase (im , of , siz ) hal_flash_erase(((uint32_t)(((im)->hdr)) + of), siz)
92+ # define wb_flash_write (im , of , dat , siz ) hal_flash_write(((uint32_t)((im)->hdr)) + of, dat, siz)
93+ # define wb_flash_write_verify_word (im , of , x ) do { hal_flash_write(((uint32_t)((im)->hdr)) + of, (void *)&x, sizeof(uint32_t)); } while (*(uint32_t *)(((im)->hdr) + of) != x)
94+ #endif /* EXT_FLASH */
95+
6296#endif /* IMAGE_H */
0 commit comments