blob: ada22e8747db4d57b194fd45282e8ae173b9ea91 [file] [log] [blame]
Patrick Rudolpha892cde2018-04-19 14:39:07 +02001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2003-2004 Eric Biederman
5 * Copyright (C) 2005-2010 coresystems GmbH
6 * Copyright (C) 2014 Google Inc.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; version 2 of the License.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 */
17
18#include <console/console.h>
19#include <bootmem.h>
20#include <cbmem.h>
21#include <device/resource.h>
22#include <stdlib.h>
23#include <commonlib/region.h>
24#include <fit.h>
25#include <program_loading.h>
26#include <timestamp.h>
27#include <cbfs.h>
28#include <string.h>
29#include <commonlib/compression.h>
30#include <lib.h>
31#include <fit_payload.h>
32
33/* Pack the device_tree and place it at given position. */
34static void pack_fdt(struct region *fdt, struct device_tree *dt)
35{
36 printk(BIOS_INFO, "FIT: Flattening FDT to %p\n",
37 (void *)fdt->offset);
38
39 dt_flatten(dt, (void *)fdt->offset);
40 prog_segment_loaded(fdt->offset, fdt->size, 0);
41}
42
43/**
44 * Extract a node to given regions.
45 * Returns true on error, false on success.
46 */
47static bool extract(struct region *region, struct fit_image_node *node)
48{
49 void *dst = (void *)region->offset;
50 const char *comp_name;
51 size_t true_size = 0;
52
53 switch (node->compression) {
54 case CBFS_COMPRESS_NONE:
55 comp_name = "Relocating uncompressed";
56 break;
57 case CBFS_COMPRESS_LZMA:
58 comp_name = "Decompressing LZMA";
59 break;
60 case CBFS_COMPRESS_LZ4:
61 comp_name = "Decompressing LZ4";
62 break;
63 default:
64 printk(BIOS_ERR, "ERROR: Unsupported compression\n");
65 return true;
66 }
67
68 printk(BIOS_INFO, "FIT: %s %s to %p\n", comp_name, node->name, dst);
69
70 switch (node->compression) {
71 case CBFS_COMPRESS_NONE:
72 memcpy(dst, node->data, node->size);
73 true_size = node->size;
74 break;
75 case CBFS_COMPRESS_LZMA:
76 timestamp_add_now(TS_START_ULZMA);
77 true_size = ulzman(node->data, node->size, dst, region->size);
78 timestamp_add_now(TS_END_ULZMA);
79 break;
80 case CBFS_COMPRESS_LZ4:
81 timestamp_add_now(TS_START_ULZ4F);
82 true_size = ulz4fn(node->data, node->size, dst, region->size);
83 timestamp_add_now(TS_END_ULZ4F);
84 break;
85 default:
86 return true;
87 }
88
89 if (!true_size) {
90 printk(BIOS_ERR, "ERROR: %s node failed!\n", comp_name);
91 return true;
92 }
93
94 prog_segment_loaded(region->offset, true_size, 0);
95
96 return false;
97}
98
99/*
100 * Parse the uImage FIT, choose a configuration and extract images.
101 */
102void fit_payload(struct prog *payload)
103{
104 struct device_tree *dt = NULL;
105 struct region kernel = {0}, fdt = {0}, initrd = {0};
106 void *data;
107
108 data = rdev_mmap_full(prog_rdev(payload));
109
110 if (data == NULL)
111 return;
112
113 printk(BIOS_INFO, "FIT: Examine payload %s\n", payload->name);
114
115 struct fit_config_node *config = fit_load(data);
116
117 if (!config || !config->kernel_node) {
118 printk(BIOS_ERR, "ERROR: Could not load FIT\n");
119 rdev_munmap(prog_rdev(payload), data);
120 return;
121 }
122
123 if (config->fdt_node) {
124 dt = fdt_unflatten(config->fdt_node->data);
125 if (!dt) {
126 printk(BIOS_ERR,
127 "ERROR: Failed to unflatten the FDT.\n");
128 rdev_munmap(prog_rdev(payload), data);
129 return;
130 }
131
132 dt_apply_fixups(dt);
133
134 /* Update device_tree */
135#if defined(CONFIG_LINUX_COMMAND_LINE)
136 fit_update_chosen(dt, (char *)CONFIG_LINUX_COMMAND_LINE);
137#endif
138 fit_update_memory(dt);
139 }
140
141 /* Collect infos for fit_payload_arch */
142 kernel.size = config->kernel_node->size;
143 fdt.size = dt ? dt_flat_size(dt) : 0;
144 initrd.size = config->ramdisk_node ? config->ramdisk_node->size : 0;
145
146 /* Invoke arch specific payload placement and fixups */
147 if (!fit_payload_arch(payload, config, &kernel, &fdt, &initrd)) {
148 printk(BIOS_ERR, "ERROR: Failed to find free memory region\n");
149 bootmem_dump_ranges();
150 rdev_munmap(prog_rdev(payload), data);
151 return;
152 }
153
154 /* Load the images to given position */
155 if (config->fdt_node) {
156 /* Update device_tree */
157 if (config->ramdisk_node)
158 fit_add_ramdisk(dt, (void *)initrd.offset, initrd.size);
159
160 pack_fdt(&fdt, dt);
161 }
162
163 if (config->ramdisk_node &&
164 extract(&initrd, config->ramdisk_node)) {
165 printk(BIOS_ERR, "ERROR: Failed to extract initrd\n");
166 rdev_munmap(prog_rdev(payload), data);
167 return;
168 }
169
170 timestamp_add_now(TS_KERNEL_DECOMPRESSION);
171
172 if (extract(&kernel, config->kernel_node)) {
173 printk(BIOS_ERR, "ERROR: Failed to extract kernel\n");
174 rdev_munmap(prog_rdev(payload), data);
175 return;
176 }
177
178 timestamp_add_now(TS_START_KERNEL);
179
180 rdev_munmap(prog_rdev(payload), data);
181}