mgmt/imgmgr/src/imgmgr.c
int
imgr_read_info(int image_slot, struct image_version *ver, uint8_t *hash,
               uint32_t *flags)
{
    struct image_header *hdr1;
    struct image_header_v2 *hdr2;
    struct image_tlv *tlv;
    int rc = -1;
    int rc2;
    const struct flash_area *fa;
    uint8_t data[sizeof(struct image_header)];
    uint32_t data_off, data_end;
    int area_id;
    bool is_v2_image = false;

    /* Silence spurious warning. */
    data_end = 0;

    area_id = flash_area_id_from_image_slot(image_slot);

    hdr1 = (struct image_header *)data;
    hdr2 = (struct image_header_v2 *)data;
    rc2 = flash_area_open(area_id, &fa);
    if (rc2) {
        return -1;
    }
    /* image_header  and image_header_v2 are the same size */
    rc2 = flash_area_read_is_empty(fa, 0, hdr1, sizeof(*hdr1));
    if (rc2 < 0) {
        goto end;
    }

    if (ver) {
        memset(ver, 0xff, sizeof(*ver));
    }
    if (hdr1->ih_magic == IMAGE_MAGIC) {
        if (ver) {
            memcpy(ver, &hdr1->ih_ver, sizeof(*ver));
        }
    } else if (hdr1->ih_magic == IMAGE_MAGIC_V2) {
        is_v2_image = true;
        if (ver) {
            memcpy(ver, &hdr2->ih_ver, sizeof(*ver));
        }
    } else if (rc2 == 1) {
        /* Area is empty */
        rc = 2;
        goto end;
    } else {
        rc = 1;
        goto end;
    }

    if (flags) {
        if (hdr1->ih_magic == IMAGE_MAGIC) {
            *flags = hdr1->ih_flags;
        } else {
            *flags = hdr2->ih_flags;
        }
    }

    /*
     * Build ID is in a TLV after the image.
     */
    if (hdr1->ih_magic == IMAGE_MAGIC_V1) {
        data_off = hdr1->ih_hdr_size + hdr1->ih_img_size;
    } else {
        data_off = hdr2->ih_hdr_size + hdr2->ih_img_size;
    }

    rc = imgr_img_tlvs(fa, hdr1, &data_off, &data_end);
    if (rc) {
        goto end;
    }

    if (data_end > fa->fa_size) {
        rc = 1;
        goto end;
    }
    tlv = (struct image_tlv *)data;
    while (data_off + sizeof(*tlv) <= data_end) {
        rc2 = flash_area_read_is_empty(fa, data_off, tlv, sizeof(*tlv));
        if (rc2 < 0) {
            goto end;
        }
        if (rc2 == 1) {
            break;
        }
        if (!is_v2_image) {
            if (tlv->it_type != IMAGE_TLV_SHA256 ||
                tlv->it_len != IMGMGR_HASH_LEN) {
                data_off += sizeof(*tlv) + tlv->it_len;
                continue;
            }
        } else {
            /* V2 TLV */
            if (tlv->it_type != 0x10 ||
                tlv->it_len != IMGMGR_HASH_LEN) {
                data_off += sizeof(*tlv) + tlv->it_len;
                continue;
            }
        }
        data_off += sizeof(*tlv);
        if (hash) {
            if (data_off + IMGMGR_HASH_LEN > data_end) {
                goto end;
            }
            rc2 = flash_area_read(fa, data_off, hash, IMGMGR_HASH_LEN);
            if (rc2) {
                goto end;
            }
        }
        rc = 0;
        goto end;
    }
    rc = 1;
end:
    flash_area_close(fa);
    return rc;
}
/*
 * Read version and build hash from image located slot "image_slot".  Note:
 * this is a slot index, not a flash area ID.
 *
 * @param image_slot
 * @param ver (optional)
 * @param hash (optional)
 * @param flags
 *
 * Returns -1 if area is not readable.
 * Returns 0 if image in slot is ok, and version string is valid.
 * Returns 1 if there is not a full image.
 * Returns 2 if slot is empty. XXXX not there yet
 */
Read version and build hash from image located slot "image_slot".  Note:
this is a slot index, not a flash area ID.



1 if area is not readable.
Returns 0 if image in slot is ok, and version string is valid.
Returns 1 if there is not a full image.
Returns 2 if slot is empty. XXXX not there yet
Read version and build hash from image located slot " image_slot " . Note : this is a slot index not a flash area ID . 1 if area is not readable . Returns 0 if image in slot is ok and version string is valid . Returns 1 if there is not a full image . Returns 2 if slot is empty . XXXX not there yet
int
imgr_read_info(int image_slot, struct image_version *ver, uint8_t *hash,
               uint32_t *flags)
{
    struct image_header *hdr1;
    struct image_header_v2 *hdr2;
    struct image_tlv *tlv;
    int rc = -1;
    int rc2;
    const struct flash_area *fa;
    uint8_t data[sizeof(struct image_header)];
    uint32_t data_off, data_end;
    int area_id;
    bool is_v2_image = false;
    data_end = 0;
    area_id = flash_area_id_from_image_slot(image_slot);
    hdr1 = (struct image_header *)data;
    hdr2 = (struct image_header_v2 *)data;
    rc2 = flash_area_open(area_id, &fa);
    if (rc2) {
        return -1;
    }
    rc2 = flash_area_read_is_empty(fa, 0, hdr1, sizeof(*hdr1));
    if (rc2 < 0) {
        goto end;
    }
    if (ver) {
        memset(ver, 0xff, sizeof(*ver));
    }
    if (hdr1->ih_magic == IMAGE_MAGIC) {
        if (ver) {
            memcpy(ver, &hdr1->ih_ver, sizeof(*ver));
        }
    } else if (hdr1->ih_magic == IMAGE_MAGIC_V2) {
        is_v2_image = true;
        if (ver) {
            memcpy(ver, &hdr2->ih_ver, sizeof(*ver));
        }
    } else if (rc2 == 1) {
        rc = 2;
        goto end;
    } else {
        rc = 1;
        goto end;
    }
    if (flags) {
        if (hdr1->ih_magic == IMAGE_MAGIC) {
            *flags = hdr1->ih_flags;
        } else {
            *flags = hdr2->ih_flags;
        }
    }
    if (hdr1->ih_magic == IMAGE_MAGIC_V1) {
        data_off = hdr1->ih_hdr_size + hdr1->ih_img_size;
    } else {
        data_off = hdr2->ih_hdr_size + hdr2->ih_img_size;
    }
    rc = imgr_img_tlvs(fa, hdr1, &data_off, &data_end);
    if (rc) {
        goto end;
    }
    if (data_end > fa->fa_size) {
        rc = 1;
        goto end;
    }
    tlv = (struct image_tlv *)data;
    while (data_off + sizeof(*tlv) <= data_end) {
        rc2 = flash_area_read_is_empty(fa, data_off, tlv, sizeof(*tlv));
        if (rc2 < 0) {
            goto end;
        }
        if (rc2 == 1) {
            break;
        }
        if (!is_v2_image) {
            if (tlv->it_type != IMAGE_TLV_SHA256 ||
                tlv->it_len != IMGMGR_HASH_LEN) {
                data_off += sizeof(*tlv) + tlv->it_len;
                continue;
            }
        } else {
            if (tlv->it_type != 0x10 ||
                tlv->it_len != IMGMGR_HASH_LEN) {
                data_off += sizeof(*tlv) + tlv->it_len;
                continue;
            }
        }
        data_off += sizeof(*tlv);
        if (hash) {
            if (data_off + IMGMGR_HASH_LEN > data_end) {
                goto end;
            }
            rc2 = flash_area_read(fa, data_off, hash, IMGMGR_HASH_LEN);
            if (rc2) {
                goto end;
            }
        }
        rc = 0;
        goto end;
    }
    rc = 1;
end:
    flash_area_close(fa);
    return rc;
}
int imgr_read_info ( int image_slot , struct image_version * ver , uint8_t * hash , uint32_t * flags ) { struct image_header * hdr1 ; struct image_header_v2 * hdr2 ; struct image_tlv * tlv ; int rc = -1 ; int rc2 ; const struct flash_area * fa ; uint8_t data [ sizeof ( struct image_header ) ] ; uint32_t data_off , data_end ; int area_id ; bool is_v2_image = false ; data_end = 0 ; area_id = flash_area_id_from_image_slot ( image_slot ) ; hdr1 = ( struct image_header * ) data ; hdr2 = ( struct image_header_v2 * ) data ; rc2 = flash_area_open ( area_id , & fa ) ; if ( rc2 ) { return -1 ; } rc2 = flash_area_read_is_empty ( fa , 0 , hdr1 , sizeof ( * hdr1 ) ) ; if ( rc2 < 0 ) { goto end ; } if ( ver ) { memset ( ver , 0xff , sizeof ( * ver ) ) ; } if ( hdr1 -> ih_magic == IMAGE_MAGIC ) { if ( ver ) { memcpy ( ver , & hdr1 -> ih_ver , sizeof ( * ver ) ) ; } } else if ( hdr1 -> ih_magic == IMAGE_MAGIC_V2 ) { is_v2_image = true ; if ( ver ) { memcpy ( ver , & hdr2 -> ih_ver , sizeof ( * ver ) ) ; } } else if ( rc2 == 1 ) { rc = 2 ; goto end ; } else { rc = 1 ; goto end ; } if ( flags ) { if ( hdr1 -> ih_magic == IMAGE_MAGIC ) { * flags = hdr1 -> ih_flags ; } else { * flags = hdr2 -> ih_flags ; } } if ( hdr1 -> ih_magic == IMAGE_MAGIC_V1 ) { data_off = hdr1 -> ih_hdr_size + hdr1 -> ih_img_size ; } else { data_off = hdr2 -> ih_hdr_size + hdr2 -> ih_img_size ; } rc = imgr_img_tlvs ( fa , hdr1 , & data_off , & data_end ) ; if ( rc ) { goto end ; } if ( data_end > fa -> fa_size ) { rc = 1 ; goto end ; } tlv = ( struct image_tlv * ) data ; while ( data_off + sizeof ( * tlv ) <= data_end ) { rc2 = flash_area_read_is_empty ( fa , data_off , tlv , sizeof ( * tlv ) ) ; if ( rc2 < 0 ) { goto end ; } if ( rc2 == 1 ) { break ; } if ( ! is_v2_image ) { if ( tlv -> it_type != IMAGE_TLV_SHA256 || tlv -> it_len != IMGMGR_HASH_LEN ) { data_off += sizeof ( * tlv ) + tlv -> it_len ; continue ; } } else { if ( tlv -> it_type != 0x10 || tlv -> it_len != IMGMGR_HASH_LEN ) { data_off += sizeof ( * tlv ) + tlv -> it_len ; continue ; } } data_off += sizeof ( * tlv ) ; if ( hash ) { if ( data_off + IMGMGR_HASH_LEN > data_end ) { goto end ; } rc2 = flash_area_read ( fa , data_off , hash , IMGMGR_HASH_LEN ) ; if ( rc2 ) { goto end ; } } rc = 0 ; goto end ; } rc = 1 ; end : flash_area_close ( fa ) ; return rc ; }
Read version and build hash from image located slot "image_slot".
Read version and build hash from image located slot " image_slot " .
/* Silence spurious warning. */ /* image_header  and image_header_v2 are the same size */ /* Area is empty */ /*
     * Build ID is in a TLV after the image.
     */ /* V2 TLV */