Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 0fbf66b | 2017-05-18 20:09:02 -0600 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2017 Google, Inc |
| 4 | * Written by Simon Glass <sjg@chromium.org> |
Simon Glass | 0fbf66b | 2017-05-18 20:09:02 -0600 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 8 | #include <log.h> |
Masahiro Yamada | 75f82d0 | 2018-03-05 01:20:11 +0900 | [diff] [blame] | 9 | #include <linux/libfdt.h> |
Simon Glass | 0fbf66b | 2017-05-18 20:09:02 -0600 | [diff] [blame] | 10 | #include <dm/of_access.h> |
| 11 | #include <dm/of_extra.h> |
| 12 | #include <dm/ofnode.h> |
| 13 | |
Simon Glass | 4b58093 | 2018-06-11 13:07:17 -0600 | [diff] [blame] | 14 | int ofnode_read_fmap_entry(ofnode node, struct fmap_entry *entry) |
Simon Glass | 0fbf66b | 2017-05-18 20:09:02 -0600 | [diff] [blame] | 15 | { |
| 16 | const char *prop; |
Simon Glass | bf0a692 | 2021-01-21 13:57:14 -0700 | [diff] [blame^] | 17 | ofnode subnode; |
Simon Glass | 0fbf66b | 2017-05-18 20:09:02 -0600 | [diff] [blame] | 18 | |
Simon Glass | 699c9ca | 2018-10-01 12:22:08 -0600 | [diff] [blame] | 19 | if (ofnode_read_u32(node, "image-pos", &entry->offset)) { |
| 20 | debug("Node '%s' has bad/missing 'image-pos' property\n", |
Simon Glass | 4b58093 | 2018-06-11 13:07:17 -0600 | [diff] [blame] | 21 | ofnode_get_name(node)); |
Simon Glass | bf0a692 | 2021-01-21 13:57:14 -0700 | [diff] [blame^] | 22 | return log_msg_ret("image-pos", -ENOENT); |
Simon Glass | 0fbf66b | 2017-05-18 20:09:02 -0600 | [diff] [blame] | 23 | } |
Simon Glass | 699c9ca | 2018-10-01 12:22:08 -0600 | [diff] [blame] | 24 | if (ofnode_read_u32(node, "size", &entry->length)) { |
| 25 | debug("Node '%s' has bad/missing 'size' property\n", |
| 26 | ofnode_get_name(node)); |
Simon Glass | bf0a692 | 2021-01-21 13:57:14 -0700 | [diff] [blame^] | 27 | return log_msg_ret("size", -ENOENT); |
Simon Glass | 699c9ca | 2018-10-01 12:22:08 -0600 | [diff] [blame] | 28 | } |
Simon Glass | 0fbf66b | 2017-05-18 20:09:02 -0600 | [diff] [blame] | 29 | entry->used = ofnode_read_s32_default(node, "used", entry->length); |
| 30 | prop = ofnode_read_string(node, "compress"); |
Simon Glass | 699c9ca | 2018-10-01 12:22:08 -0600 | [diff] [blame] | 31 | if (prop) { |
| 32 | if (!strcmp(prop, "lz4")) |
| 33 | entry->compress_algo = FMAP_COMPRESS_LZ4; |
| 34 | else |
Simon Glass | bf0a692 | 2021-01-21 13:57:14 -0700 | [diff] [blame^] | 35 | return log_msg_ret("compression algo", -EINVAL); |
Simon Glass | 699c9ca | 2018-10-01 12:22:08 -0600 | [diff] [blame] | 36 | } else { |
| 37 | entry->compress_algo = FMAP_COMPRESS_NONE; |
| 38 | } |
| 39 | entry->unc_length = ofnode_read_s32_default(node, "uncomp-size", |
| 40 | entry->length); |
Simon Glass | bf0a692 | 2021-01-21 13:57:14 -0700 | [diff] [blame^] | 41 | subnode = ofnode_find_subnode(node, "hash"); |
| 42 | if (ofnode_valid(subnode)) { |
| 43 | prop = ofnode_read_prop(subnode, "value", &entry->hash_size); |
| 44 | |
| 45 | /* Assume it is sha256 */ |
| 46 | entry->hash_algo = prop ? FMAP_HASH_SHA256 : FMAP_HASH_NONE; |
| 47 | entry->hash = (uint8_t *)prop; |
| 48 | } |
Simon Glass | 0fbf66b | 2017-05-18 20:09:02 -0600 | [diff] [blame] | 49 | |
| 50 | return 0; |
| 51 | } |
Simon Glass | 2e4d872 | 2018-06-11 13:07:18 -0600 | [diff] [blame] | 52 | |
| 53 | int ofnode_decode_region(ofnode node, const char *prop_name, fdt_addr_t *basep, |
| 54 | fdt_size_t *sizep) |
| 55 | { |
| 56 | const fdt_addr_t *cell; |
| 57 | int len; |
| 58 | |
| 59 | debug("%s: %s: %s\n", __func__, ofnode_get_name(node), prop_name); |
| 60 | cell = ofnode_get_property(node, prop_name, &len); |
| 61 | if (!cell || (len < sizeof(fdt_addr_t) * 2)) { |
| 62 | debug("cell=%p, len=%d\n", cell, len); |
| 63 | return -1; |
| 64 | } |
| 65 | |
| 66 | *basep = fdt_addr_to_cpu(*cell); |
| 67 | *sizep = fdt_size_to_cpu(cell[1]); |
| 68 | debug("%s: base=%08lx, size=%lx\n", __func__, (ulong)*basep, |
| 69 | (ulong)*sizep); |
| 70 | |
| 71 | return 0; |
| 72 | } |
| 73 | |
| 74 | int ofnode_decode_memory_region(ofnode config_node, const char *mem_type, |
| 75 | const char *suffix, fdt_addr_t *basep, |
| 76 | fdt_size_t *sizep) |
| 77 | { |
| 78 | char prop_name[50]; |
| 79 | const char *mem; |
| 80 | fdt_size_t size, offset_size; |
| 81 | fdt_addr_t base, offset; |
| 82 | ofnode node; |
| 83 | |
| 84 | if (!ofnode_valid(config_node)) { |
| 85 | config_node = ofnode_path("/config"); |
| 86 | if (!ofnode_valid(config_node)) { |
| 87 | debug("%s: Cannot find /config node\n", __func__); |
| 88 | return -ENOENT; |
| 89 | } |
| 90 | } |
| 91 | if (!suffix) |
| 92 | suffix = ""; |
| 93 | |
| 94 | snprintf(prop_name, sizeof(prop_name), "%s-memory%s", mem_type, |
| 95 | suffix); |
| 96 | mem = ofnode_read_string(config_node, prop_name); |
| 97 | if (!mem) { |
| 98 | debug("%s: No memory type for '%s', using /memory\n", __func__, |
| 99 | prop_name); |
| 100 | mem = "/memory"; |
| 101 | } |
| 102 | |
| 103 | node = ofnode_path(mem); |
| 104 | if (!ofnode_valid(node)) { |
| 105 | debug("%s: Failed to find node '%s'\n", __func__, mem); |
| 106 | return -ENOENT; |
| 107 | } |
| 108 | |
| 109 | /* |
| 110 | * Not strictly correct - the memory may have multiple banks. We just |
| 111 | * use the first |
| 112 | */ |
| 113 | if (ofnode_decode_region(node, "reg", &base, &size)) { |
| 114 | debug("%s: Failed to decode memory region %s\n", __func__, |
| 115 | mem); |
| 116 | return -EINVAL; |
| 117 | } |
| 118 | |
| 119 | snprintf(prop_name, sizeof(prop_name), "%s-offset%s", mem_type, |
| 120 | suffix); |
| 121 | if (ofnode_decode_region(config_node, prop_name, &offset, |
| 122 | &offset_size)) { |
| 123 | debug("%s: Failed to decode memory region '%s'\n", __func__, |
| 124 | prop_name); |
| 125 | return -EINVAL; |
| 126 | } |
| 127 | |
| 128 | *basep = base + offset; |
| 129 | *sizep = offset_size; |
| 130 | |
| 131 | return 0; |
| 132 | } |