WXK
2024-09-18 05e2e954bd127de378a9d1dfbb0ed95d725aad63
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
 
#include "sysinit/sysinit.h"
#include "mgmt/mgmt.h"
#include "img_mgmt/img_mgmt_impl.h"
#include "img_mgmt/img_mgmt.h"
#include "image_map_config.h"
#include "sysflash/sysflash.h"
#include "img_mgmt/image.h"
#include "FreeRTOS.h"
#include "task.h"
 
#define SPLIT_NMGR_OP_SPLIT 0
 
/** Attempt to boot the contents of slot 0. */
#define BOOT_SWAP_TYPE_NONE     1
 
/** Swap to slot 1.  Absent a confirm command, revert back on next boot. */
#define BOOT_SWAP_TYPE_TEST     2
 
/** Swap to slot 1, and permanently switch to booting its contents. */
#define BOOT_SWAP_TYPE_PERM     3
 
/** Swap back to alternate slot.  A confirm changes this state to NONE. */
#define BOOT_SWAP_TYPE_REVERT   4
 
/** Swap failed because image to be run is not valid */
#define BOOT_SWAP_TYPE_FAIL     5
 
/** Swapping encountered an unrecoverable error */
#define BOOT_SWAP_TYPE_PANIC    0xff
 
#define MAX_FLASH_ALIGN         8
 
 
#define SYS_EOK         (0)
#define SYS_ENOMEM      (-1)
#define SYS_EINVAL      (-2)
#define SYS_ETIMEOUT    (-3)
#define SYS_ENOENT      (-4)
#define SYS_EIO         (-5)
#define SYS_EAGAIN      (-6)
#define SYS_EACCES      (-7)
#define SYS_EBUSY       (-8)
#define SYS_ENODEV      (-9)
#define SYS_ERANGE      (-10)
#define SYS_EALREADY    (-11)
#define SYS_ENOTSUP     (-12)
#define SYS_EUNKNOWN    (-13)
#define SYS_EREMOTEIO   (-14)
#define SYS_EDONE       (-15)
 
typedef enum {
    /** Loader only. */
    SPLIT_MODE_LOADER =            0,
 
    /** Loader + app; revert to loader on reboot. */
    SPLIT_MODE_TEST_APP =          1,
 
    /** Loader + app; no change on reboot. */
    SPLIT_MODE_APP =               2,
 
    /** Loader only, revert to loader + app on reboot. */
    SPLIT_MODE_TEST_LOADER =       3,
} split_mode_t;
 
struct flash_area {
    uint8_t fa_id;
    uint8_t fa_device_id;
    uint16_t pad16;
    uint32_t fa_off;
    uint32_t fa_size;
};
 
struct flash_area g_code_area;
struct flash_area g_swap_area;
 
int
flash_area_is_empty(const struct flash_area *fa, bool *empty)
{
    *empty = false;     /*always erase*/
    return 0;
}
 
int
flash_area_read(const struct flash_area *fa, uint32_t off, void *dst,
    uint32_t len)
{
    int ret;
    
    if (off > fa->fa_size || off + len > fa->fa_size) {
        ret = -1;
        return ret;
    }
    
    taskENTER_CRITICAL();
    ret = FMC_ReadStream(FLCTL, fa->fa_off + off, CMD_DREAD, dst, len);
    taskEXIT_CRITICAL();
    return ret;
}
 
int flash_area_open(uint8_t id, const struct flash_area **area)
{
    if (id == FLASH_AREA_IMAGE_0)
    {
       *area = &g_code_area;
    } else if(id == FLASH_AREA_IMAGE_1)
    {
       *area = &g_swap_area;
    }
    return 0;
}
 
int
flash_area_write(const struct flash_area *fa, uint32_t off, const void *src,
    uint32_t len)
{
    int ret;
 
    if (off > fa->fa_size || off + len > fa->fa_size) {
        ret = -1;
        return ret;
    }
 
    taskENTER_CRITICAL();
    ret = FMC_WriteStream(FLCTL, fa->fa_off + off, (void *)src, len);
    taskEXIT_CRITICAL();
    return ret;
}
 
int
flash_area_erase(const struct flash_area *fa, uint32_t off, uint32_t len)
{
    int ret;
    
    if (off > fa->fa_size || off + len > fa->fa_size) {
        ret = -1;
        return ret;
    }
   
    taskENTER_CRITICAL();
    ret = FMC_EraseCodeArea(FLCTL, fa->fa_off + off, len);
    taskEXIT_CRITICAL();
    return ret;
}
 
int
img_mgmt_impl_log_upload_done(int status, const uint8_t *hash)
{
    return 0;
}
 
int
img_mgmt_impl_log_pending(int status, const uint8_t *hash)
{
    return 0;
}
 
int
img_mgmt_impl_log_confirm(int status, const uint8_t *hash)
{
    return 0;
}
 
 
int
img_mgmt_impl_log_upload_start(int status)
{
    return 0;
}
 
 
#define flash_area_close(flash_area)
 
int boot_current_slot;
 
int boot_swap_type(void)
{
    return BOOT_SWAP_TYPE_NONE;
}
 
int boot_set_pending(int permanent)
{
    return SYS_ENOTSUP;
}
 
int boot_set_confirmed(void)
{
    return SYS_ENOTSUP;
}
 
int
split_go(int loader_slot, int split_slot, void **entry)
{
    return SYS_ENOTSUP;
}
 
int split_write_split(split_mode_t mode)
{
    return 0;
}
 
uint32_t
flash_area_erased_val(const struct flash_area *fa)
{
    return 0xff;
}
 
int flash_area_id_from_image_slot(int slot)
{
    return (slot+1);
}
 
 
void flash_area_init(void)
{
    g_code_area.fa_device_id = FLASH_AREA_IMAGE_0;
    g_code_area.fa_id = FLASH_AREA_IMAGE_0;
    g_code_area.fa_off = SIZE_BOOTLOADER;
    g_code_area.fa_size = APP_IMAGE_SIZE;
    
    g_swap_area.fa_device_id = FLASH_AREA_IMAGE_1;
    g_swap_area.fa_id = FLASH_AREA_IMAGE_1;
    g_swap_area.fa_off = (SIZE_BOOTLOADER + SIZE_FIXED_APP_IMAGE);
    g_swap_area.fa_size = APP_IMAGE_SIZE;
}
 
int split_app_active_get(void)
{
    return 0;
}
 
uint8_t
flash_area_align(const struct flash_area *fa)
{
    return 1;
}
 
static int
img_mgmt_find_best_area_id(void)
{
    #if 1
    return FLASH_AREA_IMAGE_1;
    #else
    struct image_version ver;
    int best = -1;
    int i;
    int rc;
 
    for (i = 0; i < 2; i++) {
        rc = img_mgmt_read_info(i, &ver, NULL, NULL);
        if (rc < 0) {
            continue;
        }
        if (rc == 0) {
            /* Image in slot is ok. */
            if (img_mgmt_slot_in_use(i)) {
                /* Slot is in use; can't use this. */
                continue;
            } else {
                /*
                 * Not active slot, but image is ok. Use it if there are
                 * no better candidates.
                 */
                best = i;
            }
            continue;
        }
        best = i;
        break;
    }
    if (best >= 0) {
        best = flash_area_id_from_image_slot(best);
    }
    return best;
    #endif
}
 
/**
 * Compares two image version numbers in a semver-compatible way.
 *
 * @param a                     The first version to compare.
 * @param b                     The second version to compare.
 *
 * @return                      -1 if a < b
 * @return                       0 if a = b
 * @return                       1 if a > b
 */
static int
img_mgmt_vercmp(const struct image_version *a, const struct image_version *b)
{
    if (a->iv_major < b->iv_major) {
        return -1;
    } else if (a->iv_major > b->iv_major) {
        return 1;
    }
 
    if (a->iv_minor < b->iv_minor) {
        return -1;
    } else if (a->iv_minor > b->iv_minor) {
        return 1;
    }
 
    if (a->iv_revision < b->iv_revision) {
        return -1;
    } else if (a->iv_revision > b->iv_revision) {
        return 1;
    }
 
    /* Note: For semver compatibility, don't compare the 32-bit build num. */
 
    return 0;
}
 
 
/**
 * Verifies an upload request and indicates the actions that should be taken
 * during processing of the request.  This is a "read only" function in the
 * sense that it doesn't write anything to flash and doesn't modify any global
 * variables.
 *
 * @param req                   The upload request to inspect.
 * @param action                On success, gets populated with information
 *                                  about how to process the request.
 *
 * @return                      0 if processing should occur;
 *                              A MGMT_ERR code if an error response should be
 *                                  sent instead.
 */
int
img_mgmt_impl_upload_inspect(const struct img_mgmt_upload_req *req,
                             struct img_mgmt_upload_action *action,
                             const char **errstr)
{
    const struct image_header *hdr;
    const struct flash_area *fa;
    struct image_version cur_ver;
    uint8_t rem_bytes;
    bool empty;
    int rc;
 
    memset(action, 0, sizeof *action);
 
    if (req->off == -1) {
        /* Request did not include an `off` field. */
        *errstr = img_mgmt_err_str_hdr_malformed;
        return MGMT_ERR_EINVAL;
    }
 
    if (req->off == 0) {
        /* First upload chunk. */
        if (req->data_len < sizeof(struct image_header)) {
            /*
             * Image header is the first thing in the image.
             */
            *errstr = img_mgmt_err_str_hdr_malformed;
            return MGMT_ERR_EINVAL;
        }
 
        if (req->size == -1) {
            /* Request did not include a `len` field. */
            *errstr = img_mgmt_err_str_hdr_malformed;
            return MGMT_ERR_EINVAL;
        }
        action->size = req->size;
 
        hdr = (struct image_header *)req->img_data;
        if (hdr->ih_magic != IMAGE_MAGIC) {
            *errstr = img_mgmt_err_str_magic_mismatch;
            return MGMT_ERR_EINVAL;
        }
 
        if (req->data_sha_len > IMG_MGMT_DATA_SHA_LEN) {
            return MGMT_ERR_EINVAL;
        }
 
        /*
         * If request includes proper data hash we can check whether there is
         * upload in progress (interrupted due to e.g. link disconnection) with
         * the same data hash so we can just resume it by simply including
         * current upload offset in response.
         */
        if ((req->data_sha_len > 0) && (g_img_mgmt_state.area_id != -1)) {
            if ((g_img_mgmt_state.data_sha_len == req->data_sha_len) &&
                            !memcmp(g_img_mgmt_state.data_sha, req->data_sha,
                                                        req->data_sha_len)) {
                return 0;
            }
        }
 
        action->area_id = img_mgmt_find_best_area_id();
        if (action->area_id < 0) {
            /* No slot where to upload! */
            *errstr = img_mgmt_err_str_no_slot;
            return MGMT_ERR_ENOMEM;
        }
 
        if (req->upgrade) {
            /* User specified upgrade-only.  Make sure new image version is
             * greater than that of the currently running image.
             */
            rc = img_mgmt_my_version(&cur_ver);
            if (rc != 0) {
                return MGMT_ERR_EUNKNOWN;
            }
 
            if (img_mgmt_vercmp(&cur_ver, &hdr->ih_ver) >= 0) {
                *errstr = img_mgmt_err_str_downgrade;
                return MGMT_ERR_EBADSTATE;
            }
        }
 
#if MYNEWT_VAL(IMG_MGMT_LAZY_ERASE)
        (void) empty;
#else
        rc = flash_area_open(action->area_id, &fa);
        if (rc) {
            *errstr = img_mgmt_err_str_flash_open_failed;
            return MGMT_ERR_EUNKNOWN;
        }
 
        rc = flash_area_is_empty(fa, &empty);
        flash_area_close(fa);
        if (rc) {
            return MGMT_ERR_EUNKNOWN;
        }
 
        action->erase = !empty;
#endif
    } else {
        /* Continuation of upload. */
        action->area_id = g_img_mgmt_state.area_id;
        action->size = g_img_mgmt_state.size;
 
        if (req->off != g_img_mgmt_state.off) {
            /*
             * Invalid offset. Drop the data, and respond with the offset we're
             * expecting data for.
             */
            return 0;
        }
    }
 
    /* Calculate size of flash write. */
    action->write_bytes = req->data_len;
    if (req->off + req->data_len < action->size) {
        /*
         * Respect flash write alignment if not in the last block
         */
        rc = flash_area_open(action->area_id, &fa);
        if (rc) {
            *errstr = img_mgmt_err_str_flash_open_failed;
            return MGMT_ERR_EUNKNOWN;
        }
 
        rem_bytes = req->data_len % flash_area_align(fa);
        flash_area_close(fa);
 
        if (rem_bytes) {
            action->write_bytes -= rem_bytes;
        }
    }
 
    action->proceed = true;
    return 0;
}
 
int
img_mgmt_impl_erase_slot(void)
{
    const struct flash_area *fa;
    bool empty;
    int rc;
 
    rc = flash_area_open(FLASH_AREA_IMAGE_1, &fa);
    if (rc != 0) {
        return MGMT_ERR_EUNKNOWN;
    }
 
    rc = flash_area_is_empty(fa, &empty);
    if (rc != 0) {
        return MGMT_ERR_EUNKNOWN;
    }
 
    if (!empty) {
        rc = flash_area_erase(fa, 0, fa->fa_size);
        if (rc != 0) {
            return MGMT_ERR_EUNKNOWN;
        }
    }
 
    return 0;
}
 
int
img_mgmt_impl_write_pending(int slot, bool permanent)
{
    uint32_t image_flags;
    uint8_t state_flags;
    int split_app_active;
    int rc;
 
    state_flags = img_mgmt_state_flags(slot);
    split_app_active = split_app_active_get();
 
    /* Unconfirmed slots are always runable.  A confirmed slot can only be
     * run if it is a loader in a split image setup.
     */
    if (state_flags & IMG_MGMT_STATE_F_CONFIRMED &&
        (slot != 0 || !split_app_active)) {
 
        return MGMT_ERR_EBADSTATE;
    }
 
    rc = img_mgmt_read_info(slot, NULL, NULL, &image_flags);
    if (rc != 0) {
        return MGMT_ERR_EUNKNOWN;
    }
 
    if (!(image_flags & IMAGE_F_NON_BOOTABLE)) {
        /* Unified image or loader. */
        if (!split_app_active) {
            /* No change in split status. */
            rc = boot_set_pending(permanent);
            if (rc != 0) {
                return MGMT_ERR_EUNKNOWN;
            }
        } else {
            /* Currently loader + app; testing loader-only. */
            if (permanent) {
                rc = split_write_split(SPLIT_MODE_LOADER);
            } else {
                rc = split_write_split(SPLIT_MODE_TEST_LOADER);
            }
            if (rc != 0) {
                return MGMT_ERR_EUNKNOWN;
            }
        }
    } else {
        /* Testing split app. */
        if (permanent) {
            rc = split_write_split(SPLIT_MODE_APP);
        } else {
            rc = split_write_split(SPLIT_MODE_TEST_APP);
        }
        if (rc != 0) {
            return MGMT_ERR_EUNKNOWN;
        }
    }
 
    return 0;
}
 
int
img_mgmt_impl_write_confirmed(void)
{
    int rc;
 
    /* Confirm the unified image or loader in slot 0. */
    rc = boot_set_confirmed();
    if (rc != 0) {
        return MGMT_ERR_EUNKNOWN;
    }
 
    /* If a split app in slot 1 is active, confirm it as well. */
    if (split_app_active_get()) {
        rc = split_write_split(SPLIT_MODE_APP);
        if (rc != 0) {
            return MGMT_ERR_EUNKNOWN;
        }
    } else {
        rc = split_write_split(SPLIT_MODE_LOADER);
        if (rc != 0) {
            return MGMT_ERR_EUNKNOWN;
        }
    }
 
    return 0;
}
 
int
img_mgmt_impl_read(int slot, unsigned int offset, void *dst,
                   unsigned int num_bytes)
{
    const struct flash_area *fa;
    int area_id;
    int rc;
 
    area_id = flash_area_id_from_image_slot(slot);
    rc = flash_area_open(area_id, &fa);
    if (rc != 0) {
        return MGMT_ERR_EUNKNOWN;
    }
 
    rc = flash_area_read(fa, offset, dst, num_bytes);
    flash_area_close(fa);
    if (rc != 0) {
        return MGMT_ERR_EUNKNOWN;
    }
 
    return 0;
}
 
#if MYNEWT_VAL(IMG_MGMT_LAZY_ERASE)
int
img_mgmt_impl_write_image_data(unsigned int offset, const void *data,
                               unsigned int num_bytes, bool last)
{
    const struct flash_area *fa;
    struct flash_area sector;
    int rc;
 
    rc = flash_area_open(FLASH_AREA_IMAGE_1, &fa);
    if (rc != 0) {
        return MGMT_ERR_EUNKNOWN;
    }
 
    /* Check if there any unerased target sectors, if not clean them. */
    while ((fa->fa_off + offset + num_bytes) > g_img_mgmt_state.sector_end) {
        rc = flash_area_getnext_sector(fa->fa_id, &g_img_mgmt_state.sector_id,
                                       &sector);
        if (rc) {
            goto err;
        }
        rc = flash_area_erase(&sector, 0, sector.fa_size);
        if (rc) {
            goto err;
        }
        g_img_mgmt_state.sector_end = sector.fa_off + sector.fa_size;
    }
 
    if (last) {
        g_img_mgmt_state.sector_id = -1;
        g_img_mgmt_state.sector_end = 0;
    }
 
    rc = flash_area_write(fa, offset, data, num_bytes);
    flash_area_close(fa);
    if (rc != 0) {
        return MGMT_ERR_EUNKNOWN;
    }
 
    return 0;
 
err:
    g_img_mgmt_state.sector_id = -1;
    g_img_mgmt_state.sector_end = 0;
    return MGMT_ERR_EUNKNOWN;
}
 
#else
int
img_mgmt_impl_write_image_data(unsigned int offset, const void *data,
                               unsigned int num_bytes, bool last)
{
    const struct flash_area *fa;
    int rc;
 
    rc = flash_area_open(FLASH_AREA_IMAGE_1, &fa);
    if (rc != 0) {
        return MGMT_ERR_EUNKNOWN;
    }
 
    rc = flash_area_write(fa, offset, data, num_bytes);
    flash_area_close(fa);
    if (rc != 0) {
        return MGMT_ERR_EUNKNOWN;
    }
 
    return 0;
}
#endif
 
int
img_mgmt_impl_erase_image_data(unsigned int off, unsigned int num_bytes)
{
    const struct flash_area *fa;
    int rc;
 
    rc = flash_area_open(FLASH_AREA_IMAGE_1, &fa);
    if (rc != 0) {
        return MGMT_ERR_EUNKNOWN;
    }
 
    rc = flash_area_erase(fa, off, num_bytes);
    flash_area_close(fa);
    if (rc != 0) {
        return MGMT_ERR_EUNKNOWN;
    }
 
    return 0;
}
 
#if MYNEWT_VAL(IMG_MGMT_LAZY_ERASE)
int
img_mgmt_impl_erase_if_needed(uint32_t off, uint32_t len)
{
    int rc = 0;
    struct flash_area sector;
    const struct flash_area *cfa;
 
    rc = flash_area_open(FLASH_AREA_IMAGE_1, &cfa);
    if (rc != 0) {
        return MGMT_ERR_EUNKNOWN;
    }
 
    while ((cfa->fa_off + off + len) > g_img_mgmt_state.sector_end) {
        rc = flash_area_getnext_sector(cfa->fa_id,
                                       &g_img_mgmt_state.sector_id, &sector);
        if (rc) {
            goto done;
        }
        rc = flash_area_erase(&sector, 0, sector.fa_size);
        if (rc) {
            goto done;
        }
        g_img_mgmt_state.sector_end = sector.fa_off + sector.fa_size;
    }
 
done:
    flash_area_close(cfa);
    return rc;
}
#endif
 
int
img_mgmt_impl_swap_type(int slot)
{
    assert(slot == 0 || slot == 1);
 
    switch (boot_swap_type()) {
    case BOOT_SWAP_TYPE_NONE:
        return IMG_MGMT_SWAP_TYPE_NONE;
    case BOOT_SWAP_TYPE_TEST:
        return IMG_MGMT_SWAP_TYPE_TEST;
    case BOOT_SWAP_TYPE_PERM:
        return IMG_MGMT_SWAP_TYPE_PERM;
    case BOOT_SWAP_TYPE_REVERT:
        return IMG_MGMT_SWAP_TYPE_REVERT;
    default:
        assert(0);
        return IMG_MGMT_SWAP_TYPE_NONE;
    }
}
 
int
img_mgmt_impl_erased_val(int slot, uint8_t *erased_val)
{
    const struct flash_area *fa;
    int rc;
 
    rc = flash_area_open(flash_area_id_from_image_slot(slot), &fa);
    if (rc != 0) {
      return MGMT_ERR_EUNKNOWN;
    }
 
    *erased_val = flash_area_erased_val(fa);
    flash_area_close(fa);
 
    return 0;
}
 
void
img_mgmt_module_init(void)
{
    /* Ensure this function only gets called by sysinit. */
    SYSINIT_ASSERT_ACTIVE();
    flash_area_init();
    img_mgmt_register_group();
}