dtb_object.cc revision 10880:61a56f76222b
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, 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, len, data, 48 ObjectFile::UnknownArch, ObjectFile::UnknownOpSys); 49 } else { 50 return NULL; 51 } 52} 53 54DtbObject::DtbObject(const std::string &_filename, size_t _len, uint8_t *_data, 55 Arch _arch, OpSys _opSys) 56 : ObjectFile(_filename, _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 // Make sure to clean up memory properly depending 76 // on how buffer was allocated. 77 if (fileData && !fileDataMmapped) { 78 delete [] fileData; 79 fileData = NULL; 80 } else if (fileData) { 81 munmap(fileData, len); 82 fileData = NULL; 83 } 84} 85 86bool 87DtbObject::addBootCmdLine(const char* _args, size_t len) 88{ 89 const char* root_path = "/"; 90 const char* node_name = "chosen"; 91 const char* full_path_node_name = "/chosen"; 92 const char* property_name = "bootargs"; 93 94 // Make a new buffer that has extra space to add nodes/properties 95 int newLen = 2*this->len; 96 uint8_t* fdt_buf_w_space = new uint8_t[newLen]; 97 // Copy and unpack flattened device tree into new buffer 98 int ret = fdt_open_into((void*)fileData, (void*)fdt_buf_w_space, (newLen)); 99 if (ret < 0) { 100 warn("Error resizing buffer of flattened device tree, " 101 "errno: %d\n", ret); 102 delete [] fdt_buf_w_space; 103 return false; 104 } 105 106 // First try finding the /chosen node in the dtb 107 int offset = fdt_path_offset((void*)fdt_buf_w_space, full_path_node_name); 108 if (offset < 0) { 109 // try adding the node by walking dtb tree to proper insertion point 110 offset = fdt_path_offset((void*)fdt_buf_w_space, root_path); 111 offset = fdt_add_subnode((void*)fdt_buf_w_space, offset, node_name); 112 offset = fdt_path_offset((void*)fdt_buf_w_space, full_path_node_name); 113 if (offset < 0) { 114 warn("Error finding or adding \"chosen\" subnode to flattened " 115 "device tree, errno: %d\n", offset); 116 delete [] fdt_buf_w_space; 117 return false; 118 } 119 } 120 121 // Set the bootargs property in the /chosen node 122 ret = fdt_setprop((void*)fdt_buf_w_space, offset, property_name, 123 (const void*)_args, len+1); 124 if (ret < 0) { 125 warn("Error setting \"bootargs\" property to flattened device tree, " 126 "errno: %d\n", ret); 127 delete [] fdt_buf_w_space; 128 return false; 129 } 130 131 // Repack the dtb for kernel use 132 ret = fdt_pack((void*)fdt_buf_w_space); 133 if (ret < 0) { 134 warn("Error re-packing flattened device tree structure, " 135 "errno: %d\n", ret); 136 delete [] fdt_buf_w_space; 137 return false; 138 } 139 140 text.size = newLen; 141 text.fileImage = fdt_buf_w_space; 142 143 // clean up old buffer and set to new fdt blob 144 munmap(fileData, this->len); 145 fileData = fdt_buf_w_space; 146 fileDataMmapped = false; 147 this->len = newLen; 148 149 return true; 150} 151 152Addr 153DtbObject::findReleaseAddr() 154{ 155 void *fd = (void*)fileData; 156 157 int offset = fdt_path_offset(fd, "/cpus/cpu@0"); 158 int len; 159 160 const void* temp = fdt_getprop(fd, offset, "cpu-release-addr", &len); 161 Addr rel_addr = 0; 162 163 if (len > 3) 164 rel_addr = betoh(*static_cast<const uint32_t*>(temp)); 165 if (len == 8) 166 rel_addr = (rel_addr << 32) | betoh(*(static_cast<const uint32_t*>(temp)+1)); 167 168 return rel_addr; 169} 170 171 172bool 173DtbObject::loadGlobalSymbols(SymbolTable *symtab, Addr addrMask) 174{ 175 // nothing to do here 176 return false; 177} 178 179bool 180DtbObject::loadLocalSymbols(SymbolTable *symtab, Addr addrMask) 181{ 182 // nothing to do here 183 return false; 184} 185