<commit_before>/**
 * 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 <os/endian.h>

#include <limits.h>
#include <assert.h>
#include <string.h>
#include <hal/hal_bsp.h>
#include <hal/flash_map.h>
#include <newtmgr/newtmgr.h>
#include <json/json.h>
#include <util/base64.h>

#include <bootutil/image.h>

#include "imgmgr/imgmgr.h"
#include "imgmgr_priv.h"

static int imgr_list(struct nmgr_jbuf *);
static int imgr_list2(struct nmgr_jbuf *);
static int imgr_noop(struct nmgr_jbuf *);
static int imgr_upload(struct nmgr_jbuf *);

static const struct nmgr_handler imgr_nmgr_handlers[] = {
    [IMGMGR_NMGR_OP_LIST] = {
        .nh_read = imgr_list,
        .nh_write = imgr_noop
    },
    [IMGMGR_NMGR_OP_UPLOAD] = {
        .nh_read = imgr_noop,
        .nh_write = imgr_upload
    },
    [IMGMGR_NMGR_OP_BOOT] = {
        .nh_read = imgr_boot_read,
        .nh_write = imgr_boot_write
    },
    [IMGMGR_NMGR_OP_FILE] = {
#ifdef FS_PRESENT
        .nh_read = imgr_file_download,
        .nh_write = imgr_file_upload
#else
        .nh_read = imgr_noop,
        .nh_write = imgr_noop
#endif
    },
    [IMGMGR_NMGR_OP_LIST2] = {
        .nh_read = imgr_list2,
        .nh_write = imgr_noop
    },
    [IMGMGR_NMGR_OP_BOOT2] = {
        .nh_read = imgr_boot2_read,
        .nh_write = imgr_boot2_write
    },
    [IMGMGR_NMGR_OP_CORELIST] = {
#ifdef COREDUMP_PRESENT
        .nh_read = imgr_core_list,
        .nh_write = imgr_noop,
#else
        .nh_read = imgr_noop,
        .nh_write = imgr_noop
#endif
    },
    [IMGMGR_NMGR_OP_CORELOAD] = {
#ifdef COREDUMP_PRESENT
        .nh_read = imgr_core_load,
        .nh_write = imgr_core_erase,
#else
        .nh_read = imgr_noop,
        .nh_write = imgr_noop
#endif
    }
};

static struct nmgr_group imgr_nmgr_group = {
    .ng_handlers = (struct nmgr_handler *)imgr_nmgr_handlers,
    .ng_handlers_count =
    sizeof(imgr_nmgr_handlers) / sizeof(imgr_nmgr_handlers[0]),
    .ng_group_id = NMGR_GROUP_ID_IMAGE,
};

struct imgr_state imgr_state;

/*
 * Read version and build hash from image located in flash area 'area_id'.
 *
 * 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
 */
int
imgr_read_info(int area_id, struct image_version *ver, uint8_t *hash)
{
    struct image_header *hdr;
    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;

    hdr = (struct image_header *)data;
    rc2 = flash_area_open(area_id, &fa);
    if (rc2) {
        return -1;
    }
    rc2 = flash_area_read(fa, 0, hdr, sizeof(*hdr));
    if (rc2) {
        goto end;
    }
    memset(ver, 0xff, sizeof(*ver));
    if (hdr->ih_magic == IMAGE_MAGIC) {
        memcpy(ver, &hdr->ih_ver, sizeof(*ver));
    } else if (hdr->ih_magic == 0xffffffff) {
        rc = 2;
        goto end;
    } else {
        rc = 1;
        goto end;
    }

    /*
     * Build ID is in a TLV after the image.
     */
    data_off = hdr->ih_hdr_size + hdr->ih_img_size;
    data_end = data_off + hdr->ih_tlv_size;

    tlv = (struct image_tlv *)data;
    while (data_off + sizeof(*tlv) <= data_end) {
        rc2 = flash_area_read(fa, data_off, tlv, sizeof(*tlv));
        if (rc2) {
            break;
        }
        if (tlv->it_type == 0xff && tlv->it_len == 0xffff) {
            rc = 1;
            break;
        }
        if (tlv->it_type != IMAGE_TLV_SHA256 ||
          tlv->it_len != IMGMGR_HASH_LEN) {
            data_off += sizeof(*tlv) + tlv->it_len;
            continue;
        }
        data_off += sizeof(*tlv);
        if (hash) {
            rc2 = flash_area_read(fa, data_off, hash, IMGMGR_HASH_LEN);
            if (rc2) {
                break;
            }
        }
        rc = 0;
        break;
    }
end:
    flash_area_close(fa);
    return rc;
}

int
imgr_my_version(struct image_version *ver)
{
    return imgr_read_info(bsp_imgr_current_slot(), ver, NULL);
}

/*
 * Finds image given version number. Returns the slot number image is in,
 * or -1 if not found.
 */
int
imgr_find_by_ver(struct image_version *find, uint8_t *hash)
{
    int i;
    struct image_version ver;<commit_msg>imgmgr; fix return codes of image_get_info() when it sees corrupt image.<commit_after>/**
 * 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 <os/endian.h>

#include <limits.h>
#include <assert.h>
#include <string.h>
#include <hal/hal_bsp.h>
#include <hal/flash_map.h>
#include <newtmgr/newtmgr.h>
#include <json/json.h>
#include <util/base64.h>

#include <bootutil/image.h>

#include "imgmgr/imgmgr.h"
#include "imgmgr_priv.h"

static int imgr_list(struct nmgr_jbuf *);
static int imgr_list2(struct nmgr_jbuf *);
static int imgr_noop(struct nmgr_jbuf *);
static int imgr_upload(struct nmgr_jbuf *);

static const struct nmgr_handler imgr_nmgr_handlers[] = {
    [IMGMGR_NMGR_OP_LIST] = {
        .nh_read = imgr_list,
        .nh_write = imgr_noop
    },
    [IMGMGR_NMGR_OP_UPLOAD] = {
        .nh_read = imgr_noop,
        .nh_write = imgr_upload
    },
    [IMGMGR_NMGR_OP_BOOT] = {
        .nh_read = imgr_boot_read,
        .nh_write = imgr_boot_write
    },
    [IMGMGR_NMGR_OP_FILE] = {
#ifdef FS_PRESENT
        .nh_read = imgr_file_download,
        .nh_write = imgr_file_upload
#else
        .nh_read = imgr_noop,
        .nh_write = imgr_noop
#endif
    },
    [IMGMGR_NMGR_OP_LIST2] = {
        .nh_read = imgr_list2,
        .nh_write = imgr_noop
    },
    [IMGMGR_NMGR_OP_BOOT2] = {
        .nh_read = imgr_boot2_read,
        .nh_write = imgr_boot2_write
    },
    [IMGMGR_NMGR_OP_CORELIST] = {
#ifdef COREDUMP_PRESENT
        .nh_read = imgr_core_list,
        .nh_write = imgr_noop,
#else
        .nh_read = imgr_noop,
        .nh_write = imgr_noop
#endif
    },
    [IMGMGR_NMGR_OP_CORELOAD] = {
#ifdef COREDUMP_PRESENT
        .nh_read = imgr_core_load,
        .nh_write = imgr_core_erase,
#else
        .nh_read = imgr_noop,
        .nh_write = imgr_noop
#endif
    }
};

static struct nmgr_group imgr_nmgr_group = {
    .ng_handlers = (struct nmgr_handler *)imgr_nmgr_handlers,
    .ng_handlers_count =
    sizeof(imgr_nmgr_handlers) / sizeof(imgr_nmgr_handlers[0]),
    .ng_group_id = NMGR_GROUP_ID_IMAGE,
};

struct imgr_state imgr_state;

/*
 * Read version and build hash from image located in flash area 'area_id'.
 *
 * 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
 */
int
imgr_read_info(int area_id, struct image_version *ver, uint8_t *hash)
{
    struct image_header *hdr;
    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;

    hdr = (struct image_header *)data;
    rc2 = flash_area_open(area_id, &fa);
    if (rc2) {
        return -1;
    }
    rc2 = flash_area_read(fa, 0, hdr, sizeof(*hdr));
    if (rc2) {
        goto end;
    }
    memset(ver, 0xff, sizeof(*ver));
    if (hdr->ih_magic == IMAGE_MAGIC) {
        memcpy(ver, &hdr->ih_ver, sizeof(*ver));
    } else if (hdr->ih_magic == 0xffffffff) {
        rc = 2;
        goto end;
    } else {
        rc = 1;
        goto end;
    }

    /*
     * Build ID is in a TLV after the image.
     */
    data_off = hdr->ih_hdr_size + hdr->ih_img_size;
    data_end = data_off + hdr->ih_tlv_size;

    tlv = (struct image_tlv *)data;
    while (data_off + sizeof(*tlv) <= data_end) {
        rc2 = flash_area_read(fa, data_off, tlv, sizeof(*tlv));
        if (rc2) {
            goto end;
        }
        if (tlv->it_type == 0xff && tlv->it_len == 0xffff) {
            break;
        }
        if (tlv->it_type != IMAGE_TLV_SHA256 ||
          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_my_version(struct image_version *ver)
{
    return imgr_read_info(bsp_imgr_current_slot(), ver, NULL);
}

/*
 * Finds image given version number. Returns the slot number image is in,
 * or -1 if not found.
 */
int
imgr_find_by_ver(struct image_version *find, uint8_t *hash)
{
    int i;
    struct image_version ver;
{'fraction_non_alphanumeric': 0.10541199414919551, 'fraction_numerical': 0.008971233544612385, 'mean_word_length': 2.5772584583188003, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 46, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7625629', 'n_tokens_mistral': 3782, 'n_tokens_neox': 3698, 'n_words': 944}