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 | 0fbf66b | 2017-05-18 20:09:02 -0600 | [diff] [blame] | 17 | |
Simon Glass | 699c9ca | 2018-10-01 12:22:08 -0600 | [diff] [blame] | 18 | if (ofnode_read_u32(node, "image-pos", &entry->offset)) { |
| 19 | debug("Node '%s' has bad/missing 'image-pos' property\n", |
Simon Glass | 4b58093 | 2018-06-11 13:07:17 -0600 | [diff] [blame] | 20 | ofnode_get_name(node)); |
Simon Glass | 699c9ca | 2018-10-01 12:22:08 -0600 | [diff] [blame] | 21 | return log_ret(-ENOENT); |
Simon Glass | 0fbf66b | 2017-05-18 20:09:02 -0600 | [diff] [blame] | 22 | } |
Simon Glass | 699c9ca | 2018-10-01 12:22:08 -0600 | [diff] [blame] | 23 | if (ofnode_read_u32(node, "size", &entry->length)) { |
| 24 | debug("Node '%s' has bad/missing 'size' property\n", |
| 25 | ofnode_get_name(node)); |
| 26 | return log_ret(-ENOENT); |
| 27 | } |
Simon Glass | 0fbf66b | 2017-05-18 20:09:02 -0600 | [diff] [blame] | 28 | entry->used = ofnode_read_s32_default(node, "used", entry->length); |
| 29 | prop = ofnode_read_string(node, "compress"); |
Simon Glass | 699c9ca | 2018-10-01 12:22:08 -0600 | [diff] [blame] | 30 | if (prop) { |
| 31 | if (!strcmp(prop, "lz4")) |
| 32 | entry->compress_algo = FMAP_COMPRESS_LZ4; |
| 33 | else |
| 34 | return log_msg_ret("Unknown compression algo", |
| 35 | -EINVAL); |
| 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 | 0fbf66b | 2017-05-18 20:09:02 -0600 | [diff] [blame] | 41 | prop = ofnode_read_string(node, "hash"); |
| 42 | if (prop) |
| 43 | entry->hash_size = strlen(prop); |
| 44 | entry->hash_algo = prop ? FMAP_HASH_SHA256 : FMAP_HASH_NONE; |
| 45 | entry->hash = (uint8_t *)prop; |
| 46 | |
| 47 | return 0; |
| 48 | } |
Simon Glass | 2e4d872 | 2018-06-11 13:07:18 -0600 | [diff] [blame] | 49 | |
| 50 | int ofnode_decode_region(ofnode node, const char *prop_name, fdt_addr_t *basep, |
| 51 | fdt_size_t *sizep) |
| 52 | { |
| 53 | const fdt_addr_t *cell; |
| 54 | int len; |
| 55 | |
| 56 | debug("%s: %s: %s\n", __func__, ofnode_get_name(node), prop_name); |
| 57 | cell = ofnode_get_property(node, prop_name, &len); |
| 58 | if (!cell || (len < sizeof(fdt_addr_t) * 2)) { |
| 59 | debug("cell=%p, len=%d\n", cell, len); |
| 60 | return -1; |
| 61 | } |
| 62 | |
| 63 | *basep = fdt_addr_to_cpu(*cell); |
| 64 | *sizep = fdt_size_to_cpu(cell[1]); |
| 65 | debug("%s: base=%08lx, size=%lx\n", __func__, (ulong)*basep, |
| 66 | (ulong)*sizep); |
| 67 | |
| 68 | return 0; |
| 69 | } |
| 70 | |
| 71 | int ofnode_decode_memory_region(ofnode config_node, const char *mem_type, |
| 72 | const char *suffix, fdt_addr_t *basep, |
| 73 | fdt_size_t *sizep) |
| 74 | { |
| 75 | char prop_name[50]; |
| 76 | const char *mem; |
| 77 | fdt_size_t size, offset_size; |
| 78 | fdt_addr_t base, offset; |
| 79 | ofnode node; |
| 80 | |
| 81 | if (!ofnode_valid(config_node)) { |
| 82 | config_node = ofnode_path("/config"); |
| 83 | if (!ofnode_valid(config_node)) { |
| 84 | debug("%s: Cannot find /config node\n", __func__); |
| 85 | return -ENOENT; |
| 86 | } |
| 87 | } |
| 88 | if (!suffix) |
| 89 | suffix = ""; |
| 90 | |
| 91 | snprintf(prop_name, sizeof(prop_name), "%s-memory%s", mem_type, |
| 92 | suffix); |
| 93 | mem = ofnode_read_string(config_node, prop_name); |
| 94 | if (!mem) { |
| 95 | debug("%s: No memory type for '%s', using /memory\n", __func__, |
| 96 | prop_name); |
| 97 | mem = "/memory"; |
| 98 | } |
| 99 | |
| 100 | node = ofnode_path(mem); |
| 101 | if (!ofnode_valid(node)) { |
| 102 | debug("%s: Failed to find node '%s'\n", __func__, mem); |
| 103 | return -ENOENT; |
| 104 | } |
| 105 | |
| 106 | /* |
| 107 | * Not strictly correct - the memory may have multiple banks. We just |
| 108 | * use the first |
| 109 | */ |
| 110 | if (ofnode_decode_region(node, "reg", &base, &size)) { |
| 111 | debug("%s: Failed to decode memory region %s\n", __func__, |
| 112 | mem); |
| 113 | return -EINVAL; |
| 114 | } |
| 115 | |
| 116 | snprintf(prop_name, sizeof(prop_name), "%s-offset%s", mem_type, |
| 117 | suffix); |
| 118 | if (ofnode_decode_region(config_node, prop_name, &offset, |
| 119 | &offset_size)) { |
| 120 | debug("%s: Failed to decode memory region '%s'\n", __func__, |
| 121 | prop_name); |
| 122 | return -EINVAL; |
| 123 | } |
| 124 | |
| 125 | *basep = base + offset; |
| 126 | *sizep = offset_size; |
| 127 | |
| 128 | return 0; |
| 129 | } |