dtb_object.cc revision 10508:aa46a8ae3487
1/*
2 * Copyright (c) 2013 The Regents of The University of Michigan
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 * Authors: Anthony Gutierrez
29 */
30
31#include <sys/mman.h>
32#include <err.h>
33#include <unistd.h>
34
35#include <cassert>
36
37#include "base/loader/dtb_object.hh"
38#include "sim/byteswap.hh"
39#include "fdt.h"
40#include "libfdt.h"
41
42ObjectFile *
43DtbObject::tryFile(const std::string &fname, int fd, size_t len, uint8_t *data)
44{
45    // Check if this is a FDT file by looking for magic number
46    if (fdt_magic((void*)data) == FDT_MAGIC) {
47        return new DtbObject(fname, fd, len, data,
48                             ObjectFile::UnknownArch, ObjectFile::UnknownOpSys);
49    } else {
50        return NULL;
51    }
52}
53
54DtbObject::DtbObject(const std::string &_filename, int _fd,
55                     size_t _len, uint8_t *_data,
56                     Arch _arch, OpSys _opSys)
57    : ObjectFile(_filename, _fd, _len, _data, _arch, _opSys)
58{
59    text.baseAddr = 0;
60    text.size = len;
61    text.fileImage = fileData;
62
63    data.baseAddr = 0;
64    data.size = 0;
65    data.fileImage = NULL;
66
67    bss.baseAddr = 0;
68    bss.size = 0;
69    bss.fileImage = NULL;
70
71    fileDataMmapped = true;
72}
73
74DtbObject::~DtbObject()
75{
76    if (descriptor >= 0) {
77        ::close(descriptor);
78        descriptor = -1;
79    }
80
81    // Make sure to clean up memory properly depending
82    // on how buffer was allocated.
83    if (fileData && !fileDataMmapped) {
84        delete [] fileData;
85        fileData = NULL;
86    } else if (fileData) {
87        munmap(fileData, len);
88        fileData = NULL;
89    }
90}
91
92bool
93DtbObject::addBootCmdLine(const char* _args, size_t len)
94{
95    const char* root_path = "/";
96    const char* node_name = "chosen";
97    const char* full_path_node_name = "/chosen";
98    const char* property_name = "bootargs";
99
100    // Make a new buffer that has extra space to add nodes/properties
101    int newLen = 2*this->len;
102    uint8_t* fdt_buf_w_space = new uint8_t[newLen];
103    // Copy and unpack flattened device tree into new buffer
104    int ret = fdt_open_into((void*)fileData, (void*)fdt_buf_w_space, (newLen));
105    if (ret < 0) {
106        warn("Error resizing buffer of flattened device tree, "
107             "errno: %d\n", ret);
108        delete [] fdt_buf_w_space;
109        return false;
110    }
111
112    // First try finding the /chosen node in the dtb
113    int offset = fdt_path_offset((void*)fdt_buf_w_space, full_path_node_name);
114    if (offset < 0) {
115        // try adding the node by walking dtb tree to proper insertion point
116        offset = fdt_path_offset((void*)fdt_buf_w_space, root_path);
117        offset = fdt_add_subnode((void*)fdt_buf_w_space, offset, node_name);
118        offset = fdt_path_offset((void*)fdt_buf_w_space, full_path_node_name);
119        if (offset < 0) {
120            warn("Error finding or adding \"chosen\" subnode to flattened "
121                 "device tree, errno: %d\n", offset);
122            delete [] fdt_buf_w_space;
123            return false;
124        }
125    }
126
127    // Set the bootargs property in the /chosen node
128    ret = fdt_setprop((void*)fdt_buf_w_space, offset, property_name,
129                      (const void*)_args, len+1);
130    if (ret < 0) {
131        warn("Error setting \"bootargs\" property to flattened device tree, "
132             "errno: %d\n", ret);
133        delete [] fdt_buf_w_space;
134        return false;
135    }
136
137    // Repack the dtb for kernel use
138    ret = fdt_pack((void*)fdt_buf_w_space);
139    if (ret < 0) {
140        warn("Error re-packing flattened device tree structure, "
141             "errno: %d\n", ret);
142        delete [] fdt_buf_w_space;
143        return false;
144    }
145
146    text.size = newLen;
147    text.fileImage = fdt_buf_w_space;
148
149    // clean up old buffer and set to new fdt blob
150    munmap(fileData, this->len);
151    fileData = fdt_buf_w_space;
152    fileDataMmapped = false;
153    this->len = newLen;
154
155    return true;
156}
157
158Addr
159DtbObject::findReleaseAddr()
160{
161    void *fd = (void*)fileData;
162
163    int offset = fdt_path_offset(fd, "/cpus/cpu@0");
164    int len;
165
166    const void* temp = fdt_getprop(fd, offset, "cpu-release-addr", &len);
167    Addr rel_addr = 0;
168
169    if (len > 3)
170        rel_addr = betoh(*static_cast<const uint32_t*>(temp));
171    if (len == 8)
172        rel_addr = (rel_addr << 32) | betoh(*(static_cast<const uint32_t*>(temp)+1));
173
174    return rel_addr;
175}
176
177
178bool
179DtbObject::loadGlobalSymbols(SymbolTable *symtab, Addr addrMask)
180{
181    // nothing to do here
182    return false;
183}
184
185bool
186DtbObject::loadLocalSymbols(SymbolTable *symtab, Addr addrMask)
187{
188    // nothing to do here
189    return false;
190}
191