]> git.ozlabs.org Git - petitboot/blobdiff - lib/flash/flash.c
discover/grub2: Allow to separate the --id argument using a space char
[petitboot] / lib / flash / flash.c
index b6188a07a3340d05def3b73300d2c39d201deb0a..3505cb4f959be52b5cf9068bb6e7b6359f980e83 100644 (file)
 #include <libflash/file.h>
 #include <libflash/ecc.h>
 
+#define SECURE_BOOT_HEADERS_SIZE 4096
+#define ROM_MAGIC_NUMBER 0x17082011
 
 struct flash_info {
        /* Device information */
        struct blocklevel_device        *bl;
        struct ffs_handle               *ffs;
-       uint32_t                        size;   /* raw size of partition */
+       uint64_t                        size;   /* raw size of partition */
        const char                      *path;
        bool                            ecc;
        uint32_t                        erase_granule;
@@ -82,26 +84,26 @@ static struct flash_info *flash_setup_buffer(void *ctx, const char *partition)
        rc = arch_flash_init(&info->bl, NULL, true);
        if (rc) {
                pb_log("Failed to init mtd device\n");
-               return NULL;
+               goto out;
        }
 
        rc = blocklevel_get_info(info->bl, &info->path, &info->size,
                                 &info->erase_granule);
        if (rc) {
                pb_log("Failed to retrieve blocklevel info\n");
-               return NULL;
+               goto out_flash;
        }
 
        rc = ffs_init(0, info->size, info->bl, &info->ffs, 1);
        if (rc) {
-               pb_log("%s: Failed to init ffs\n", __func__);
-               goto out;
+               pb_log_fn("Failed to init ffs\n");
+               goto out_flash;
        }
 
        rc = partition_info(info, partition);
        if (rc) {
                pb_log("Failed to retrieve partition info\n");
-               goto out;
+               goto out_ffs;
        }
 
        /* Check if there is a second flash side. If there is not, or
@@ -124,7 +126,7 @@ static struct flash_info *flash_setup_buffer(void *ctx, const char *partition)
 
        pb_debug("%s Details\n", partition);
        pb_debug("\tName\t\t%s\n", info->path);
-       pb_debug("\tFlash Size\t%u\n", info->size);
+       pb_debug("\tFlash Size\t%lu\n", info->size);
        pb_debug("\tGranule\t\t%u\n", info->erase_granule);
        pb_debug("\tECC\t\t%s\n", info->ecc ? "Protected" : "Unprotected");
        pb_debug("\tCurrent Side info:\n");
@@ -139,12 +141,25 @@ static struct flash_info *flash_setup_buffer(void *ctx, const char *partition)
        }
 
        return info;
-out:
+out_ffs:
+       ffs_close(info->ffs);
+out_flash:
        arch_flash_close(info->bl, NULL);
+out:
        talloc_free(info);
        return NULL;
 }
 
+/* See stb_is_container() in Skiboot */
+static bool is_signed(char *buffer, uint32_t len)
+{
+       if (!buffer || len <= SECURE_BOOT_HEADERS_SIZE)
+               return false;
+       if (be32_to_cpu(*(uint32_t *)buffer) != ROM_MAGIC_NUMBER)
+               return false;
+       return true;
+}
+
 int flash_parse_version(void *ctx, char ***versions, bool current)
 {
        char *saveptr, *tok,  **tmp, *buffer;
@@ -168,7 +183,7 @@ int flash_parse_version(void *ctx, char ***versions, bool current)
        len = cur_info->attr_data_len -  ecc_size(cur_info->attr_data_len);
        buffer = talloc_array(cur_info, char, len);
        if (!buffer) {
-               pb_log("%s: Failed to init buffer!\n", __func__);
+               pb_log_fn("Failed to init buffer!\n");
                goto out;
        }
 
@@ -179,12 +194,16 @@ int flash_parse_version(void *ctx, char ***versions, bool current)
                goto out;
        }
 
+       /* Check if this partition is signed */
+       if (is_signed(buffer, len))
+               buffer += SECURE_BOOT_HEADERS_SIZE;
+
        /* open-power-platform */
        tok = strtok_r(buffer, delim, &saveptr);
        if (tok) {
                tmp = talloc_realloc(ctx, tmp, char *, n + 1);
                if (!tmp) {
-                       pb_log("%s: Failed to allocate memory\n", __func__);
+                       pb_log_fn("Failed to allocate memory\n");
                        goto out;
                }
                tmp[n++] = talloc_strdup(ctx, tok);
@@ -195,7 +214,7 @@ int flash_parse_version(void *ctx, char ***versions, bool current)
                /* Ignore leading tab from subsequent lines */
                tmp = talloc_realloc(ctx, tmp, char *, n + 1);
                if (!tmp) {
-                       pb_log("%s: Failed to reallocate memory\n", __func__);
+                       pb_log_fn("Failed to reallocate memory\n");
                        n = 0;
                        goto out;
                }