@@ -17,7 +17,7 @@ const UICR_CUSTOMER_UPY_OFFSET: number = 0x40;
17
17
const UICR_UPY_START : number =
18
18
UICR_START + UICR_CUSTOMER_OFFSET + UICR_CUSTOMER_UPY_OFFSET ;
19
19
20
- const UPY_MAGIC_HEADER : number = 0x17eeb07c ;
20
+ const UPY_MAGIC_HEADER : number [ ] = [ 0x17eeb07c ] ;
21
21
const UPY_DELIMITER : number = 0xffffffff ;
22
22
const UPY_REGIONS_TERMINATOR : number = 0x00000000 ;
23
23
@@ -46,6 +46,7 @@ enum MicropythonUicrAddress {
46
46
/** MicroPython data stored in the UICR Customer area. */
47
47
interface MicropythonUicrData {
48
48
flashPageSize : number ;
49
+ flashSize : number ;
49
50
runtimeStartPage : number ;
50
51
runtimeStartAddress : number ;
51
52
runtimeEndUsed : number ;
@@ -101,13 +102,13 @@ function getStringFromIntelHexMap(
101
102
address : number
102
103
) : string {
103
104
const memBlock = intelHexMap . slice ( address ) . get ( address ) ;
104
- let i = 0 ;
105
- for ( i = 0 ; i < memBlock . length && memBlock [ i ] !== 0 ; i ++ ) ;
106
- if ( i === memBlock . length ) {
105
+ let iStrEnd = 0 ;
106
+ while ( iStrEnd < memBlock . length && memBlock [ iStrEnd ] !== 0 ) iStrEnd ++ ;
107
+ if ( iStrEnd === memBlock . length ) {
107
108
// Could not find a null character to indicate the end of the string
108
109
return '' ;
109
110
}
110
- const stringBytes = memBlock . slice ( 0 , i ) ;
111
+ const stringBytes = memBlock . slice ( 0 , iStrEnd ) ;
111
112
return bytesToStr ( stringBytes ) ;
112
113
}
113
114
@@ -119,11 +120,33 @@ function getStringFromIntelHexMap(
119
120
* @return True if the magic number matches, false otherwise.
120
121
*/
121
122
function confirmMagicValue ( intelHexMap : MemoryMap ) : boolean {
122
- const readMagicHeader : number = getUint32FromIntelHexMap (
123
+ const readMagicHeader = getMagicValue ( intelHexMap ) ;
124
+ return UPY_MAGIC_HEADER . includes ( readMagicHeader ) ;
125
+ }
126
+
127
+ /**
128
+ * Reads the UICR data that contains the Magic Value that indicates the
129
+ * MicroPython presence in the hex data.
130
+ *
131
+ * @param intelHexMap - Memory map of the Intel Hex data.
132
+ * @returns The Magic Value from UICR.
133
+ */
134
+ function getMagicValue ( intelHexMap : MemoryMap ) : number {
135
+ return getUint32FromIntelHexMap (
123
136
intelHexMap ,
124
137
MicropythonUicrAddress . MagicValue
125
138
) ;
126
- return readMagicHeader === UPY_MAGIC_HEADER ;
139
+ }
140
+
141
+ /**
142
+ * Reads the UICR data from an Intel Hex map and retrieves the flash size.
143
+ *
144
+ * @param intelHexMap - Memory map of the Intel Hex data.
145
+ * @returns The micro:bit flash size.
146
+ */
147
+ function getFlashSize ( intelHexMap : MemoryMap ) : number {
148
+ // This is the micro:bit flash size
149
+ return 0x40000 ;
127
150
}
128
151
129
152
/**
@@ -196,18 +219,20 @@ function getHexMapUicrData(intelHexMap: MemoryMap): MicropythonUicrData {
196
219
if ( ! confirmMagicValue ( uicrMap ) ) {
197
220
throw new Error ( 'Could not find valid MicroPython UICR data.' ) ;
198
221
}
199
- const pageSize : number = getPageSize ( uicrMap ) ;
222
+ const flashPageSize : number = getPageSize ( uicrMap ) ;
223
+ const flashSize : number = getFlashSize ( uicrMap ) ;
200
224
const startPage : number = getStartPage ( uicrMap ) ;
201
225
const pagesUsed : number = getPagesUsed ( uicrMap ) ;
202
226
const versionAddress : number = getVersionLocation ( uicrMap ) ;
203
227
const version : string = getStringFromIntelHexMap ( intelHexMap , versionAddress ) ;
204
228
205
229
return {
206
- flashPageSize : pageSize ,
230
+ flashPageSize,
231
+ flashSize,
207
232
runtimeStartPage : startPage ,
208
- runtimeStartAddress : startPage * pageSize ,
233
+ runtimeStartAddress : startPage * flashPageSize ,
209
234
runtimeEndUsed : pagesUsed ,
210
- runtimeEndAddress : pagesUsed * pageSize ,
235
+ runtimeEndAddress : pagesUsed * flashPageSize ,
211
236
uicrStartAddress : MicropythonUicrAddress . MagicValue ,
212
237
uicrEndAddress : MicropythonUicrAddress . End ,
213
238
versionAddress,
0 commit comments