Simon Glass | 15d320d | 2025-01-15 18:27:07 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * Verified Boot for Embedded (VBE) common functions |
| 4 | * |
| 5 | * Copyright 2024 Google LLC |
| 6 | * Written by Simon Glass <sjg@chromium.org> |
| 7 | */ |
| 8 | |
Simon Glass | 9846e5f | 2025-01-15 18:27:08 -0700 | [diff] [blame^] | 9 | #include <blk.h> |
| 10 | #include <memalign.h> |
| 11 | #include <spl.h> |
Simon Glass | 15d320d | 2025-01-15 18:27:07 -0700 | [diff] [blame] | 12 | #include "vbe_common.h" |
| 13 | |
| 14 | int vbe_get_blk(const char *storage, struct udevice **blkp) |
| 15 | { |
| 16 | struct blk_desc *desc; |
| 17 | char devname[16]; |
| 18 | const char *end; |
| 19 | int devnum; |
| 20 | |
| 21 | /* First figure out the block device */ |
| 22 | log_debug("storage=%s\n", storage); |
| 23 | devnum = trailing_strtoln_end(storage, NULL, &end); |
| 24 | if (devnum == -1) |
| 25 | return log_msg_ret("num", -ENODEV); |
| 26 | if (end - storage >= sizeof(devname)) |
| 27 | return log_msg_ret("end", -E2BIG); |
| 28 | strlcpy(devname, storage, end - storage + 1); |
| 29 | log_debug("dev=%s, %x\n", devname, devnum); |
| 30 | |
| 31 | desc = blk_get_dev(devname, devnum); |
| 32 | if (!desc) |
| 33 | return log_msg_ret("get", -ENXIO); |
| 34 | *blkp = desc->bdev; |
| 35 | |
| 36 | return 0; |
| 37 | } |
Simon Glass | 9846e5f | 2025-01-15 18:27:08 -0700 | [diff] [blame^] | 38 | |
| 39 | int vbe_read_version(struct udevice *blk, ulong offset, char *version, |
| 40 | int max_size) |
| 41 | { |
| 42 | ALLOC_CACHE_ALIGN_BUFFER(u8, buf, MMC_MAX_BLOCK_LEN); |
| 43 | |
| 44 | /* we can use an assert() here since we already read only one block */ |
| 45 | assert(max_size <= MMC_MAX_BLOCK_LEN); |
| 46 | |
| 47 | /* |
| 48 | * we can use an assert() here since reading the wrong block will just |
| 49 | * cause an invalid version-string to be (safely) read |
| 50 | */ |
| 51 | assert(!(offset & (MMC_MAX_BLOCK_LEN - 1))); |
| 52 | |
| 53 | offset /= MMC_MAX_BLOCK_LEN; |
| 54 | |
| 55 | if (blk_read(blk, offset, 1, buf) != 1) |
| 56 | return log_msg_ret("read", -EIO); |
| 57 | strlcpy(version, buf, max_size); |
| 58 | log_debug("version=%s\n", version); |
| 59 | |
| 60 | return 0; |
| 61 | } |