@@ -235,8 +235,14 @@ static int samd_protect_check(struct flash_bank *bank)
235
235
return res ;
236
236
237
237
/* Lock bits are active-low */
238
- for (int i = 0 ; i < bank -> num_sectors ; i ++ )
239
- bank -> sectors [i ].is_protected = !(lock & (1 <<i ));
238
+ int boot_sectors = bank -> num_sectors - SAMD_NUM_SECTORS ;
239
+ for (int i = 0 ; i < bank -> num_sectors ; i ++ ) {
240
+ if (i < boot_sectors ) {
241
+ bank -> sectors [i ].is_protected = !(lock & (1 <<0 ));
242
+ } else {
243
+ bank -> sectors [i ].is_protected = !(lock & (1 <<(i - boot_sectors )));
244
+ }
245
+ }
240
246
241
247
return ERROR_OK ;
242
248
}
@@ -304,17 +310,41 @@ static int samd_probe(struct flash_bank *bank)
304
310
part -> flash_kb , chip -> num_pages , chip -> page_size );
305
311
}
306
312
313
+ /* Partition the first page into subpages, to allow debugging if boot sector is protected
314
+ * The minimum boot page allowed is 2kb */
315
+ int boot_sectors = chip -> sector_size / 2048 ;
316
+ int boot_offset = 0 ;
317
+
318
+ if (boot_sectors == 1 ) {
319
+ boot_sectors = 0 ;
320
+ }
321
+
307
322
/* Allocate the sector table */
308
- bank -> num_sectors = SAMD_NUM_SECTORS ;
323
+ if (boot_sectors > 0 ) {
324
+ LOG_INFO ("SAMD: partitioning the first flash page into %d subpages" ,
325
+ boot_sectors );
326
+ bank -> num_sectors = SAMD_NUM_SECTORS + boot_sectors - 1 ;
327
+ boot_offset = chip -> sector_size ;
328
+ } else {
329
+ bank -> num_sectors = SAMD_NUM_SECTORS ;
330
+ }
331
+
309
332
bank -> sectors = calloc (bank -> num_sectors , sizeof ((bank -> sectors )[0 ]));
310
333
if (!bank -> sectors )
311
334
return ERROR_FAIL ;
312
335
313
- /* Fill out the sector information: all SAMD sectors are the same size and
314
- * there is always a fixed number of them. */
315
- for (int i = 0 ; i < bank -> num_sectors ; i ++ ) {
336
+ /* Will be skipped if SAMD_BOOT_SECTORS == 0 */
337
+ for (int i = 0 ; i < boot_sectors ; i ++ ) {
338
+ bank -> sectors [i ].size = chip -> sector_size / boot_sectors ;
339
+ bank -> sectors [i ].offset = i * chip -> sector_size / boot_sectors ;
340
+ /* mark as unknown */
341
+ bank -> sectors [i ].is_erased = -1 ;
342
+ bank -> sectors [i ].is_protected = -1 ;
343
+ }
344
+
345
+ for (int i = boot_sectors ; i < bank -> num_sectors ; i ++ ) {
316
346
bank -> sectors [i ].size = chip -> sector_size ;
317
- bank -> sectors [i ].offset = i * chip -> sector_size ;
347
+ bank -> sectors [i ].offset = (( i - boot_sectors ) * chip -> sector_size ) + boot_offset ;
318
348
/* mark as unknown */
319
349
bank -> sectors [i ].is_erased = -1 ;
320
350
bank -> sectors [i ].is_protected = -1 ;
0 commit comments