aboutsummaryrefslogtreecommitdiff
path: root/secure_fw/partitions/firmware_update/bootloader/mcuboot/tfm_mcuboot_fwu.c
blob: 08195122fbf14385ec2a76994964acaaf7d72c74 (plain)
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
/*
 * Copyright (c) 2021, Arm Limited. All rights reserved.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 *
 */
#include <string.h>
#include "psa/crypto.h"
#include "tfm_sp_log.h"
#include "bootutil_priv.h"
#include "bootutil/bootutil.h"
#include "bootutil/image.h"
#include "flash_map_backend/flash_map_backend.h"
#include "sysflash/sysflash.h"
#include "tfm_bootloader_fwu_abstraction.h"
#include "tfm_fwu_req_mngr.h"
#include "tfm_boot_status.h"
#include "service_api.h"
#include "tfm_secure_api.h"

#if (MCUBOOT_IMAGE_NUMBER == 1)
#define MAX_IMAGE_INFO_LENGTH    (sizeof(struct image_version) + \
                                  SHARED_DATA_ENTRY_HEADER_SIZE)
#else
#define MAX_IMAGE_INFO_LENGTH    2 * (sizeof(struct image_version) + \
                                      SHARED_DATA_ENTRY_HEADER_SIZE)
#endif
#define TFM_MCUBOOT_FWU_INVALID_IMAGE_ID    0xFF
/*
 * \struct fwu_image_info_data
 *
 * \brief Contains the received boot status information from bootloader
 *
 * \details This is a redefinition of \ref tfm_boot_data to allocate the
 *          appropriate, service dependent size of \ref boot_data.
 */
typedef struct fwu_image_info_data_s {
    struct shared_data_tlv_header header;
    uint8_t data[MAX_IMAGE_INFO_LENGTH];
} fwu_image_info_data_t;

typedef struct tfm_fwu_mcuboot_ctx_s {
    /* The flash area corresponding to mcuboot_image_id. */
    const struct flash_area *fap;
    uint8_t mcuboot_image_id;

    /* The size of the downloaded data in the FWU process. */
    size_t loaded_size;
} tfm_fwu_mcuboot_ctx_t;

static tfm_fwu_mcuboot_ctx_t mcuboot_ctx[TFM_FWU_MAX_IMAGES];
static fwu_image_info_data_t __attribute__((aligned(4))) boot_shared_data;

static int convert_id_from_bl_to_mcuboot(bl_image_id_t bl_image_id,
                                         uint8_t *mcuboot_image_id)
{
#if (MCUBOOT_IMAGE_NUMBER == 1)
    /* Only full image upgrade is supported in this case. */
    if (bl_image_id != FWU_IMAGE_TYPE_FULL) {
        LOG_ERRFMT("TFM FWU: multi-image is not supported in current mcuboot configuration.");
        return -1;
    }

    /* The image id in mcuboot. 0: the full image. */
    *mcuboot_image_id = 0;
#else
    if (bl_image_id == FWU_IMAGE_TYPE_SECURE) {
        /* The image id in mcuboot. 0: the secure image. */
        *mcuboot_image_id = 0;
    } else if (bl_image_id == FWU_IMAGE_TYPE_NONSECURE) {
        /* The image id in mcuboot. 1: the non-secure image. */
        *mcuboot_image_id = 1;
    }  else {
        LOG_ERRFMT("TFM FWU: invalid image_type: %d", bl_image_id);
        return -1;
    }
#endif
    return 0;
}

#if (MCUBOOT_IMAGE_NUMBER > 1)
static int convert_id_from_mcuboot_to_bl(uint8_t mcuboot_image_id,
                                         bl_image_id_t *bl_image_id)
{
    uint8_t image_type;

    if (bl_image_id == NULL) {
        return -1;
    }

    if (mcuboot_image_id == 0) {
        /* The image id in mcuboot. 0: the secure image. */
        image_type = FWU_IMAGE_TYPE_SECURE;
    } else if (mcuboot_image_id == 1) {
        /* The image id in mcuboot. 1: the non-secure image. */
        image_type = FWU_IMAGE_TYPE_NONSECURE;
    }  else {
        LOG_ERRFMT("TFM FWU: invalid mcuboot image id\n\r: %d",
                mcuboot_image_id);
        return -1;
    }

    *bl_image_id = image_type;
    return 0;
}
#endif

/*
 * Get the flash area of the image mcuboot_image_id.
 */
static bool get_flash_image_index(uint8_t mcuboot_image_id, uint8_t *index)
{
    for (uint8_t i = 0; i < TFM_FWU_MAX_IMAGES; i++) {
        if (mcuboot_ctx[i].mcuboot_image_id == mcuboot_image_id) {
            *index = i;
            return true;
        }
    }
    return false;
}

static bool get_next_free_index(uint8_t *index)
{
    for (uint8_t i = 0; i < TFM_FWU_MAX_IMAGES; i++) {
        if (mcuboot_ctx[i].fap == NULL) {
            *index = i;
            return true;
        }
    }
    return false;
}

static int fwu_bootloader_get_shared_data(void)
{
    return tfm_core_get_boot_data(TLV_MAJOR_FWU,
                                  (struct tfm_boot_data *)&boot_shared_data,
                                  sizeof(boot_shared_data));
}

static psa_status_t get_running_image_version(uint8_t mcuboot_image_id,
                                              struct image_version *image_ver)
{
    struct shared_data_tlv_entry tlv_entry;
    uint8_t *tlv_end;
    uint8_t *tlv_curr;

    /* The bootloader writes the image version information into the memory which
     * is shared between MCUboot and TF-M. Read the shared memory.
     */
    if (boot_shared_data.header.tlv_magic != SHARED_DATA_TLV_INFO_MAGIC) {
        return PSA_ERROR_GENERIC_ERROR;
    }

    tlv_end = (uint8_t *)&boot_shared_data +
                boot_shared_data.header.tlv_tot_len;
    tlv_curr = boot_shared_data.data;

    while (tlv_curr < tlv_end) {
        (void)memcpy(&tlv_entry, tlv_curr, SHARED_DATA_ENTRY_HEADER_SIZE);
        if ((GET_FWU_CLAIM(tlv_entry.tlv_type) == SW_VERSION) &&
            (GET_FWU_MODULE(tlv_entry.tlv_type) == mcuboot_image_id)) {
            if (tlv_entry.tlv_len != sizeof(struct image_version)) {
                return PSA_ERROR_GENERIC_ERROR;
            }
            memcpy(image_ver,
                tlv_curr + SHARED_DATA_ENTRY_HEADER_SIZE,
                tlv_entry.tlv_len);
            return PSA_SUCCESS;
        }
        tlv_curr += SHARED_DATA_ENTRY_HEADER_SIZE + tlv_entry.tlv_len;
    }

    return PSA_ERROR_GENERIC_ERROR;
}

psa_status_t fwu_bootloader_init(void)
{
    if (fwu_bootloader_get_shared_data() != TFM_SUCCESS) {
        return PSA_ERROR_GENERIC_ERROR;
    }
    /* add Init of specific flash driver */
    flash_area_driver_init();
    return PSA_SUCCESS;
}

psa_status_t fwu_bootloader_staging_area_init(bl_image_id_t bootloader_image_id)
{
    uint8_t mcuboot_image_id = 0;
    uint8_t index;
    const struct flash_area *fap;

    if (convert_id_from_bl_to_mcuboot(bootloader_image_id,
                                      &mcuboot_image_id) != 0) {
        return PSA_ERROR_INVALID_ARGUMENT;
    }

    if (flash_area_open(FLASH_AREA_IMAGE_SECONDARY(mcuboot_image_id),
                        &fap) != 0) {
        LOG_ERRFMT("TFM FWU: opening flash failed.\r\n");
        return PSA_ERROR_GENERIC_ERROR;
    }

    if (get_flash_image_index(mcuboot_image_id, &index) ||
        get_next_free_index(&index)) {
        mcuboot_ctx[index].mcuboot_image_id = mcuboot_image_id;
        mcuboot_ctx[index].fap = fap;

        /* Reset the loaded_size. */
        mcuboot_ctx[index].loaded_size = 0;
    } else {
        return PSA_ERROR_INSUFFICIENT_MEMORY;
    }

    if (flash_area_erase(fap, 0, fap->fa_size) != 0) {
        LOG_ERRFMT("TFM FWU: erasing flash failed.\r\n");
        return PSA_ERROR_GENERIC_ERROR;
    }

    return PSA_SUCCESS;
}

psa_status_t fwu_bootloader_load_image(bl_image_id_t bootloader_image_id,
                                       size_t block_offset,
                                       const void *block,
                                       size_t block_size)
{
    uint8_t mcuboot_image_id = 0;
    uint8_t index;
    const struct flash_area *fap;

    if (block == NULL) {
        return PSA_ERROR_INVALID_ARGUMENT;
    }

    if (convert_id_from_bl_to_mcuboot(bootloader_image_id,
                                      &mcuboot_image_id) != 0) {
        return PSA_ERROR_INVALID_ARGUMENT;
    }

    /* The image should already be added into the mcuboot_ctx. */
    if (get_flash_image_index(mcuboot_image_id, &index)) {
        fap = mcuboot_ctx[index].fap;
    } else {
        return PSA_ERROR_INVALID_ARGUMENT;
    }

    if (flash_area_write(fap, block_offset, block, block_size) != 0) {
        LOG_ERRFMT("TFM FWU: write flash failed.\r\n");
        return PSA_ERROR_GENERIC_ERROR;
    }

    /* The overflow check has been done in flash_area_write. */
    mcuboot_ctx[index].loaded_size += block_size;
    return PSA_SUCCESS;
}

#if (MCUBOOT_IMAGE_NUMBER > 1)
/**
 * \brief Compare image version numbers not including the build number.
 *
 * \param[in] image_ver_1 The first image version to compare.
 *
 * \param[in] image_ver_2 The second image version to compare.
 *
 * \return true         image_ver_1 is greater or equal than image_ver_2.
 * \return false        image_ver_1 is less than image_ver_2.
 */
static bool is_version_greater_or_equal(const struct image_version *image_ver_1,
                                        const struct image_version *image_ver_2)
{
    if (image_ver_1->iv_major > image_ver_2->iv_major) {
        return true;
    }
    if (image_ver_1->iv_major < image_ver_2->iv_major) {
        return false;
    }
    /* The major version numbers are equal, continue comparison. */
    if (image_ver_1->iv_minor > image_ver_2->iv_minor) {
        return true;
    }
    if (image_ver_1->iv_minor < image_ver_2->iv_minor) {
        return false;
    }
    /* The minor version numbers are equal, continue comparison. */
    if (image_ver_1->iv_revision >= image_ver_2->iv_revision) {
        return true;
    }
    return false;
}
#endif

psa_status_t fwu_bootloader_install_image(bl_image_id_t bootloader_image_id,
                                          bl_image_id_t *dependency,
                                        psa_image_version_t *dependency_version)
{
    uint8_t mcuboot_image_id = 0;
    const struct flash_area *fap;
    uint8_t index;
    psa_status_t ret;
#if (MCUBOOT_IMAGE_NUMBER > 1)
    struct image_tlv_iter it;
    struct image_header hdr;
    int rc;
    uint32_t off;
    uint16_t len;
    struct image_dependency dep;
    struct image_version image_ver = { 0 };
    const struct flash_area *fap_secondary;
    struct image_header hdr_secondary;
    uint8_t boot_magic[BOOT_MAGIC_SZ];
    bool check_pass = false;
#endif

    if ((dependency == NULL || dependency_version == NULL)) {
        return PSA_ERROR_INVALID_ARGUMENT;
    }

    if (convert_id_from_bl_to_mcuboot(bootloader_image_id,
                                      &mcuboot_image_id) != 0) {
        return PSA_ERROR_INVALID_ARGUMENT;
    }

    /* The image should already be added into the mcuboot_ctx. */
    if (get_flash_image_index(mcuboot_image_id, &index)) {
        fap = mcuboot_ctx[index].fap;
    } else {
        return PSA_ERROR_INVALID_ARGUMENT;
    }

#if (MCUBOOT_IMAGE_NUMBER > 1)
    /* Read the image header. */
    if (flash_area_read(fap, 0, &hdr, sizeof(hdr)) != 0) {
        return PSA_ERROR_GENERIC_ERROR;
    }

    /* Return PSA_ERROR_GENERIC_ERROR if the image header is invalid. */
    if (hdr.ih_magic != IMAGE_MAGIC) {
        return PSA_ERROR_GENERIC_ERROR;
    }

    /* Initialize the iterator. */
    if (bootutil_tlv_iter_begin(&it, &hdr, fap, IMAGE_TLV_DEPENDENCY, true)) {
        return PSA_ERROR_GENERIC_ERROR;
    }

    /* Iterate and verify the image dependencies. */
    while (true) {
        rc = bootutil_tlv_iter_next(&it, &off, &len, NULL);
        if (rc < 0) {
            return PSA_ERROR_GENERIC_ERROR;
        } else if (rc > 0) {
            /* No more dependency found. */
            rc = 0;
            ret = PSA_SUCCESS_REBOOT;
            break;
        }
        check_pass = false;

        /* A dependency requirement is found. */
        if (flash_area_read(fap, off, &dep, len) != 0) {
            return PSA_ERROR_GENERIC_ERROR;
        }

        if (dep.image_id > MCUBOOT_IMAGE_NUMBER) {
            return PSA_ERROR_GENERIC_ERROR;
        }

        /* As this partition does not validate the image in the secondary slot,
         * so it has no information of which image will be chosen to run after
         * reboot. So if the dependency image in the primary slot or that in the
         * secondary slot can meet the dependency requirement, then the
         * dependency check pass.
         */
        /* Check the dependency image in the primary slot. */
        if (get_running_image_version(dep.image_id,
                                      &image_ver) != PSA_SUCCESS) {
            return PSA_ERROR_GENERIC_ERROR;
        }

        /* Check whether the version of the running image can meet the
         * dependency requirement.
         */
        if (is_version_greater_or_equal(&image_ver,
                                        &dep.image_min_version)) {
            check_pass = true;
        } else {
            /* The running image cannot meet the dependency requirement. Check
             * the dependency image in the secondary slot.
             */
            if ((flash_area_open(FLASH_AREA_IMAGE_SECONDARY(dep.image_id),
                                 &fap_secondary)) != 0) {
                return PSA_ERROR_GENERIC_ERROR;
            }
            if (flash_area_read(fap_secondary,
                                0,
                                &hdr_secondary,
                                sizeof(hdr_secondary)) != 0) {
                flash_area_close(fap_secondary);
                return PSA_ERROR_GENERIC_ERROR;
            }

            /* Check the version of the dependency image in the secondary slot
             * only if the image header, as well as the boot magic, is good.
             */
            if (hdr_secondary.ih_magic == IMAGE_MAGIC) {
                /* Check the boot magic. */
                if (flash_area_read(fap_secondary,
                                    fap_secondary->fa_size - BOOT_MAGIC_SZ,
                                    boot_magic,
                                    BOOT_MAGIC_SZ) != 0) {
                    flash_area_close(fap_secondary);
                    return PSA_ERROR_GENERIC_ERROR;
                }
                if ((memcmp(boot_magic, &boot_img_magic, BOOT_MAGIC_SZ) == 0) &&
                    (is_version_greater_or_equal(&hdr_secondary.ih_ver,
                                                 &dep.image_min_version))) {
                    /* The dependency image in the secondary slot meet the
                     * dependency requirement.
                     */
                    check_pass = true;
                }
            }
            flash_area_close(fap_secondary);
        }
        if (!check_pass) {
            if (convert_id_from_mcuboot_to_bl(dep.image_id,
                                              dependency) != 0) {
                return PSA_ERROR_GENERIC_ERROR;
            }

            /* Return the first dependency check failed image. */
            memcpy(dependency_version,
                   &dep.image_min_version,
                   sizeof(*dependency_version));
            ret = PSA_ERROR_DEPENDENCY_NEEDED;
            break;
        }
    }
#else
    /* No dependency check is needed in single image case. */
    ret = PSA_SUCCESS_REBOOT;
#endif

    /* Write the boot magic in the image trailer so that this image will be
     * taken as a candidate. Note that even if a dependency is required, the
     * boot magic should still be set. Therefore when circular dependency exists
     * the firmware update will not enter the loop of returning
     * PSA_ERROR_DEPENDENCY_NEEDED when installing. When all the dependencies
     * are installed, the user should call the psa_fwu_install API to install
     * this image again.
     */
    if (boot_set_pending_multi(mcuboot_image_id, false) != 0) {
        return PSA_ERROR_GENERIC_ERROR;
    } else {
        return ret;
    }
}

psa_status_t fwu_bootloader_mark_image_accepted(
                                              bl_image_id_t bootloader_image_id)
{
    /* As RAM_LOAD and OVERWRITE_ONLY do not support image revert, the image
     * does not need to be confirmed explicitly in these two upgrade strategies.
     * Image revert is supported in SWAP upgrade strategy and DIRECT_XIP upgrade
     * strategy when MCUBOOT_DIRECT_XIP_REVERT is true. In these cases, the
     * image needs to be set as a permanent image explicitly. Then the accpeted
     * image can still be selected as the running image during next time reboot
     * up. Otherwise, the image will be reverted and the previous one will be
     * chosen as the running image.
     */
#if (defined(MCUBOOT_DIRECT_XIP) && defined(MCUBOOT_DIRECT_XIP_REVERT)) || \
    defined(MCUBOOT_SWAP_USING_SCRATCH) || defined(MCUBOOT_SWAP_USING_MOVE)
    uint8_t mcuboot_image_id = 0;

    if (convert_id_from_bl_to_mcuboot(bootloader_image_id,
                                    &mcuboot_image_id) != 0) {
        return PSA_ERROR_INVALID_ARGUMENT;
    }

    if (boot_set_confirmed_multi(mcuboot_image_id) != 0) {
        return PSA_ERROR_GENERIC_ERROR;
    }
#else
    (void)bootloader_image_id;
#endif
    return PSA_SUCCESS;
}

psa_status_t fwu_bootloader_abort(bl_image_id_t bootloader_image_id)
{
    uint8_t mcuboot_image_id = 0;
    const struct flash_area *fap;
    uint8_t index;

    if (convert_id_from_bl_to_mcuboot(bootloader_image_id,
                                      &mcuboot_image_id) != 0) {
        return PSA_ERROR_INVALID_ARGUMENT;
    }

    /* The image should already be added into the mcuboot_ctx. */
    if (get_flash_image_index(mcuboot_image_id, &index)) {
        fap = mcuboot_ctx[index].fap;
    } else {
        return PSA_ERROR_INVALID_ARGUMENT;
    }

    flash_area_erase(fap, 0, fap->fa_size);
    flash_area_close(fap);
    mcuboot_ctx[index].fap = NULL;
    mcuboot_ctx[index].loaded_size = 0;
    return PSA_SUCCESS;
}


static psa_status_t util_img_hash(const struct flash_area *fap,
                                 size_t data_size,
                                 uint8_t *hash_result,
                                 size_t buf_size,
                                 size_t *hash_size)
{
    psa_hash_operation_t handle = psa_hash_operation_init();
    psa_status_t status;
    uint8_t tmpbuf[BOOT_TMPBUF_SZ];
    uint32_t tmp_buf_sz = BOOT_TMPBUF_SZ;
    uint32_t blk_sz;
    uint32_t off;

    /* Setup the hash object for the desired hash. */
    status = psa_hash_setup(&handle, PSA_ALG_SHA_256);
    if (status != PSA_SUCCESS) {
        return status;
    }

    for (off = 0; off < data_size; off += blk_sz) {
        blk_sz = data_size - off;
        if (blk_sz > tmp_buf_sz) {
            blk_sz = tmp_buf_sz;
        }

        if (flash_area_read(fap, off, tmpbuf, blk_sz)) {
            return PSA_ERROR_GENERIC_ERROR;
        }
        status = psa_hash_update(&handle, tmpbuf, blk_sz);
        if (status != PSA_SUCCESS) {
            return status;
        }
    }

    status = psa_hash_finish(&handle, hash_result, buf_size, hash_size);

    return status;
}

static psa_status_t get_secondary_image_info(uint8_t image_id,
                                             psa_image_info_t *info)
{
    const struct flash_area *fap = NULL;
    struct image_header hdr = {0};
    uint8_t hash[PSA_FWU_MAX_DIGEST_SIZE] = {0};
    size_t hash_size = 0;
    psa_status_t ret = PSA_SUCCESS;
    uint8_t index;
    size_t data_size;

    if (info == NULL) {
        return PSA_ERROR_INVALID_ARGUMENT;
    }

    if ((flash_area_open(FLASH_AREA_IMAGE_SECONDARY(image_id),
                            &fap)) != 0) {
        LOG_ERRFMT("TFM FWU: opening flash failed.\r\n");
        return PSA_ERROR_GENERIC_ERROR;
    }

    if (flash_area_read(fap, 0, &hdr, sizeof(hdr)) != 0) {
        flash_area_close(fap);
        LOG_ERRFMT("TFM FWU: reading flash failed.\r\n");
        return PSA_ERROR_GENERIC_ERROR;
    }


    /* Check if the image is in a FWU process. */
    if (get_flash_image_index(image_id, &index)) {

        /* Calculate hash on the downloaded data. */
        data_size = mcuboot_ctx[index].loaded_size;
    } else {
        /* Check if a valid image exists on the staging area.
         * If a valid image exists, the FWU partition has no information on the
         * image size downloaded as the image is copied from the running slot
         * during the reboot. So in this case when calculating the image hash,
         * the range starts from the image header till the end of the protected
         * TLV area.
         */
        if (hdr.ih_magic == IMAGE_MAGIC) {
            info->version.iv_major = hdr.ih_ver.iv_major;
            info->version.iv_minor = hdr.ih_ver.iv_minor;
            info->version.iv_revision = hdr.ih_ver.iv_revision;
            info->version.iv_build_num = hdr.ih_ver.iv_build_num;
            LOG_ERRFMT("version= %d., %d., %d.,+ %d\n\r",
                    info->version.iv_major,
                    info->version.iv_minor,
                    info->version.iv_revision,
                    info->version.iv_build_num);

            /* Calculate hash on from the image header to the protected TLV. */
            data_size = hdr.ih_hdr_size + hdr.ih_img_size +
                        hdr.ih_protect_tlv_size;
        } else {
            /* No image in the staging area. */
            return PSA_ERROR_DOES_NOT_EXIST;
        }
    }

    if (util_img_hash(fap, data_size, hash, (size_t)PSA_FWU_MAX_DIGEST_SIZE,
                      &hash_size) == PSA_SUCCESS) {
        memcpy(info->digest, hash, hash_size);
        ret = PSA_SUCCESS;
    } else {
        ret = PSA_ERROR_GENERIC_ERROR;
    }

    /* The actual image state will be filled in the tfm_fwu_req_mngr.c where
     * the image state is maintained.
     */
    info->state = PSA_IMAGE_UNDEFINED;
    flash_area_close(fap);
    return ret;
}

psa_status_t fwu_bootloader_get_image_info(bl_image_id_t bootloader_image_id,
                                  bool active_image,
                                  psa_image_info_t *info)
{
    struct image_version image_ver = { 0 };
    uint8_t mcuboot_image_id = 0;
#if !defined(MCUBOOT_DIRECT_XIP) && !defined(MCUBOOT_RAM_LOAD) && \
    !defined(MCUBOOT_OVERWRITE_ONLY)
    const struct flash_area *fap = NULL;
    uint8_t image_ok = BOOT_FLAG_UNSET;
#endif
    if (info == NULL) {
        return PSA_ERROR_INVALID_ARGUMENT;
    }

    if (convert_id_from_bl_to_mcuboot(bootloader_image_id,
                                      &mcuboot_image_id) != 0) {
        return PSA_ERROR_INVALID_ARGUMENT;
    }

    memset(info, 0, sizeof(psa_image_info_t));
    memset(info->digest, TFM_IMAGE_INFO_INVALID_DIGEST, sizeof(info->digest));

    if (active_image) {
    /* As DIRECT_XIP, RAM_LOAD and OVERWRITE_ONLY do not support image revert.
     * So the running image is in INSTALLED state in these three upgrade
     * strategies. In the SWAP case, the image_ok flag should be read to check
     * whether the running image has been confirmed as a pernament image.
     */
#if !defined(MCUBOOT_DIRECT_XIP) && !defined(MCUBOOT_RAM_LOAD) && \
    !defined(MCUBOOT_OVERWRITE_ONLY)
        if ((flash_area_open(FLASH_AREA_IMAGE_PRIMARY(mcuboot_image_id),
                            &fap)) != 0) {
            LOG_ERRFMT("TFM FWU: opening flash failed.\r\n");
            return PSA_ERROR_GENERIC_ERROR;
        }

        /* Get value of image-ok flag of the image to check whether application
         * itself is already confirmed.
         */
        if (boot_read_image_ok(fap, &image_ok) != 0) {
            return PSA_ERROR_GENERIC_ERROR;
        }
        if (image_ok == BOOT_FLAG_SET) {
            info->state = PSA_IMAGE_INSTALLED;
        } else {
            info->state = PSA_IMAGE_PENDING_INSTALL;
        }
#else
        info->state = PSA_IMAGE_INSTALLED;
#endif
        if (get_running_image_version(mcuboot_image_id,
                                      &image_ver) == PSA_SUCCESS) {
            info->version.iv_major = image_ver.iv_major;
            info->version.iv_minor = image_ver.iv_minor;
            info->version.iv_revision = image_ver.iv_revision;
            info->version.iv_build_num = image_ver.iv_build_num;

            /* The image in the primary slot is verified by the bootloader.
             * The image digest in the primary slot should not be exposed to
             * nonsecure.
             */
            return PSA_SUCCESS;
        } else {
            return PSA_ERROR_GENERIC_ERROR;
        }
    } else {
        return get_secondary_image_info(mcuboot_image_id, info);
    }
}