dtb_object.cc revision 9538:182d67b5b57a
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 "fdt.h"
39#include "libfdt.h"
40
41ObjectFile *
42DtbObject::tryFile(const std::string &fname, int fd, size_t len, uint8_t *data)
43{
44    // Check if this is a FDT file by looking for magic number
45    if (fdt_magic((void*)data) == FDT_MAGIC) {
46        return new DtbObject(fname, fd, len, data,
47                             ObjectFile::UnknownArch, ObjectFile::UnknownOpSys);
48    } else {
49        return NULL;
50    }
51}
52
53DtbObject::DtbObject(const std::string &_filename, int _fd,
54                     size_t _len, uint8_t *_data,
55                     Arch _arch, OpSys _opSys)
56    : ObjectFile(_filename, _fd, _len, _data, _arch, _opSys)
57{
58    text.baseAddr = 0;
59    text.size = len;
60    text.fileImage = fileData;
61
62    data.baseAddr = 0;
63    data.size = 0;
64    data.fileImage = NULL;
65
66    bss.baseAddr = 0;
67    bss.size = 0;
68    bss.fileImage = NULL;
69
70    fileDataMmapped = true;
71}
72
73DtbObject::~DtbObject()
74{
75    if (descriptor >= 0) {
76        ::close(descriptor);
77        descriptor = -1;
78    }
79
80    // Make sure to clean up memory properly depending
81    // on how buffer was allocated.
82    if (fileData && !fileDataMmapped) {
83        delete [] fileData;
84        fileData = NULL;
85    } else if (fileData) {
86        munmap(fileData, len);
87        fileData = NULL;
88    }
89}
90
91bool
92DtbObject::addBootCmdLine(const char* _args, size_t len)
93{
94    const char* root_path = "/";
95    const char* node_name = "chosen";
96    const char* full_path_node_name = "/chosen";
97    const char* property_name = "bootargs";
98
99    // Make a new buffer that has extra space to add nodes/properties
100    int newLen = 2*this->len;
101    uint8_t* fdt_buf_w_space = new uint8_t[newLen];
102    // Copy and unpack flattened device tree into new buffer
103    int ret = fdt_open_into((void*)fileData, (void*)fdt_buf_w_space, (newLen));
104    if (ret < 0) {
105        warn("Error resizing buffer of flattened device tree, "
106             "errno: %d\n", ret);
107        delete [] fdt_buf_w_space;
108        return false;
109    }
110
111    // First try finding the /chosen node in the dtb
112    int offset = fdt_path_offset((void*)fdt_buf_w_space, full_path_node_name);
113    if (offset < 0) {
114        // try adding the node by walking dtb tree to proper insertion point
115        offset = fdt_path_offset((void*)fdt_buf_w_space, root_path);
116        offset = fdt_add_subnode((void*)fdt_buf_w_space, offset, node_name);
117        offset = fdt_path_offset((void*)fdt_buf_w_space, full_path_node_name);
118        if (offset < 0) {
119            warn("Error finding or adding \"chosen\" subnode to flattened "
120                 "device tree, errno: %d\n", offset);
121            delete [] fdt_buf_w_space;
122            return false;
123        }
124    }
125
126    // Set the bootargs property in the /chosen node
127    ret = fdt_setprop((void*)fdt_buf_w_space, offset, property_name,
128                      (const void*)_args, len+1);
129    if (ret < 0) {
130        warn("Error setting \"bootargs\" property to flattened device tree, "
131             "errno: %d\n", ret);
132        delete [] fdt_buf_w_space;
133        return false;
134    }
135
136    // Repack the dtb for kernel use
137    ret = fdt_pack((void*)fdt_buf_w_space);
138    if (ret < 0) {
139        warn("Error re-packing flattened device tree structure, "
140             "errno: %d\n", ret);
141        delete [] fdt_buf_w_space;
142        return false;
143    }
144
145    text.size = newLen;
146    text.fileImage = fdt_buf_w_space;
147
148    // clean up old buffer and set to new fdt blob
149    munmap(fileData, this->len);
150    fileData = fdt_buf_w_space;
151    fileDataMmapped = false;
152    this->len = newLen;
153
154    return true;
155}
156
157bool
158DtbObject::loadGlobalSymbols(SymbolTable *symtab, Addr addrMask)
159{
160    // nothing to do here
161    return false;
162}
163
164bool
165DtbObject::loadLocalSymbols(SymbolTable *symtab, Addr addrMask)
166{
167    // nothing to do here
168    return false;
169}
170