Masahisa Kojima | d822255 | 2023-11-10 13:25:36 +0900 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * blkmap helper function |
| 4 | * |
| 5 | * Copyright (c) 2023, Linaro Limited |
| 6 | */ |
| 7 | |
| 8 | #include <blk.h> |
| 9 | #include <blkmap.h> |
| 10 | #include <dm/device.h> |
| 11 | #include <dm/device-internal.h> |
| 12 | |
| 13 | int blkmap_create_ramdisk(const char *label, ulong image_addr, ulong image_size, |
| 14 | struct udevice **devp) |
| 15 | { |
| 16 | int ret; |
| 17 | lbaint_t blknum; |
| 18 | struct blkmap *bm; |
| 19 | struct blk_desc *desc; |
| 20 | struct udevice *bm_dev; |
| 21 | |
| 22 | ret = blkmap_create(label, &bm_dev); |
| 23 | if (ret) { |
| 24 | log_err("failed to create blkmap\n"); |
| 25 | return ret; |
| 26 | } |
| 27 | |
| 28 | bm = dev_get_plat(bm_dev); |
| 29 | desc = dev_get_uclass_plat(bm->blk); |
| 30 | blknum = image_size >> desc->log2blksz; |
| 31 | ret = blkmap_map_pmem(bm_dev, 0, blknum, image_addr); |
| 32 | if (ret) { |
| 33 | log_err("Unable to map %#llx at block %d : %d\n", |
| 34 | (unsigned long long)image_addr, 0, ret); |
| 35 | goto err; |
| 36 | } |
| 37 | log_info("Block %d+0x" LBAF " mapped to %#llx\n", 0, blknum, |
| 38 | (unsigned long long)image_addr); |
| 39 | |
| 40 | ret = device_probe(bm->blk); |
| 41 | if (ret) |
| 42 | goto err; |
| 43 | |
| 44 | if (devp) |
| 45 | *devp = bm_dev; |
| 46 | |
| 47 | return 0; |
| 48 | |
| 49 | err: |
| 50 | blkmap_destroy(bm_dev); |
| 51 | |
| 52 | return ret; |
| 53 | } |