diff options
Diffstat (limited to 'futility')
-rw-r--r-- | futility/futility.h | 3 | ||||
-rw-r--r-- | futility/misc.c | 25 | ||||
-rw-r--r-- | futility/updater.c | 15 |
3 files changed, 30 insertions, 13 deletions
diff --git a/futility/futility.h b/futility/futility.h index f0635750..f5f6877e 100644 --- a/futility/futility.h +++ b/futility/futility.h @@ -108,6 +108,9 @@ int futil_looks_like_gbb(GoogleBinaryBlockHeader *gbb, uint32_t len); int futil_valid_gbb_header(GoogleBinaryBlockHeader *gbb, uint32_t len, uint32_t *maxlen); +/* Sets the HWID string field inside a GBB header. */ +int futil_set_gbb_hwid(struct vb2_gbb_header *gbb, const char *hwid); + /* For GBB v1.2 and later, update the hwid_digest */ void update_hwid_digest(GoogleBinaryBlockHeader *gbb); diff --git a/futility/misc.c b/futility/misc.c index 703033c5..af291a49 100644 --- a/futility/misc.c +++ b/futility/misc.c @@ -4,6 +4,7 @@ * found in the LICENSE file. */ +#include <assert.h> #include <errno.h> #ifndef HAVE_MACOS #include <linux/fs.h> /* For BLKGETSIZE64 */ @@ -166,6 +167,7 @@ int print_hwid_digest(GoogleBinaryBlockHeader *gbb, return is_valid; } +/* Deprecated. Use futil_set_gbb_hwid in future. */ /* For GBB v1.2 and later, update the hwid_digest field. */ void update_hwid_digest(GoogleBinaryBlockHeader *gbb) { @@ -181,6 +183,29 @@ void update_hwid_digest(GoogleBinaryBlockHeader *gbb) gbb->hwid_digest, sizeof(gbb->hwid_digest)); } +/* Sets the HWID string field inside a GBB header. */ +int futil_set_gbb_hwid(struct vb2_gbb_header *gbb, const char *hwid) +{ + uint8_t *to = (uint8_t *)gbb + gbb->hwid_offset; + size_t len; + + assert(hwid); + len = strlen(hwid); + if (len >= gbb->hwid_size) + return -1; + + /* Zero whole area so we won't have garbage after NUL. */ + memset(to, 0, gbb->hwid_size); + memcpy(to, hwid, len); + + /* major_version starts from 1 and digest must be updated since v1.2. */ + if (gbb->major_version == 1 && gbb->minor_version < 2) + return 0; + + return vb2_digest_buffer(to, len, VB2_HASH_SHA256, gbb->hwid_digest, + sizeof(gbb->hwid_digest)); +} + /* * TODO: All sorts of race conditions likely here, and everywhere this is used. * Do we care? If so, fix it. diff --git a/futility/updater.c b/futility/updater.c index d16fe38a..d1352ef1 100644 --- a/futility/updater.c +++ b/futility/updater.c @@ -972,8 +972,6 @@ static int preserve_gbb(const struct firmware_image *image_from, struct firmware_image *image_to, int preserve_flags) { - int len; - uint8_t *hwid_to, *hwid_from; const struct vb2_gbb_header *gbb_from; struct vb2_gbb_header *gbb_to; @@ -988,18 +986,9 @@ static int preserve_gbb(const struct firmware_image *image_from, if (preserve_flags) gbb_to->flags = gbb_from->flags; - hwid_to = (uint8_t *)gbb_to + gbb_to->hwid_offset; - hwid_from = (uint8_t *)gbb_from + gbb_from->hwid_offset; - /* Preserve HWID. */ - len = strlen((const char *)hwid_from); - if (len >= gbb_to->hwid_size) - return -1; - - /* Zero whole area so we won't have garbage after NUL. */ - memset(hwid_to, 0, gbb_to->hwid_size); - memcpy(hwid_to, hwid_from, len); - return 0; + return futil_set_gbb_hwid( + gbb_to, (const char *)gbb_from + gbb_from->hwid_offset); } /* |