qualcomm/qbox
Loading...
Searching...
No Matches
loader.h
1/*
2 * Copyright (c) 2022-2024 Qualcomm Innovation Center, Inc. All Rights Reserved.
3 * Author: GreenSocs 2022
4 *
5 * SPDX-License-Identifier: BSD-3-Clause
6 */
7
8#ifndef _GREENSOCS_BASE_COMPONENTS_LOADER_H
9#define _GREENSOCS_BASE_COMPONENTS_LOADER_H
10
11#include <fstream>
12
13#include <cci_configuration>
14#include <systemc>
15#include <tlm>
16#include <tlm_utils/simple_initiator_socket.h>
17#include <scp/report.h>
18#include <module_factory_registery.h>
19#include <ports/target-signal-socket.h>
20#include <tlm_sockets_buswidth.h>
21
22#include "onmethod.h"
23#include "elf_loader.h"
24#include "zip_loader.h"
25#include "dtb_loader.h"
26
27#include <cinttypes>
28#include <list>
29#include <string>
30#include <vector>
31#include <limits>
32#include <filesystem>
33
34#ifndef _WIN32
35#include <fcntl.h>
36#include <sys/mman.h>
37#include <sys/stat.h>
38#endif
39
40#define BINFILE_READ_CHUNK_SIZE (1024 * 1024)
41
42#ifdef _WIN32
43// On Windows, libzip subsitutes close with _close.
44// We disable this behaviour that breaks calling close methods.
45#undef close
46#endif
47
48namespace gs {
49
57
58/* simple class to load csv file*/
59class CSVRow
60{
61public:
62 std::string operator[](std::size_t index) const
63 {
64 return std::string(&m_line[m_data[index] + 1], m_data[index + 1] - (m_data[index] + 1));
65 }
66 std::size_t size() const { return m_data.size() - 1; }
67 void readNextRow(std::istream& str)
68 {
69 std::getline(str, m_line);
70
71 m_data.clear();
72 m_data.emplace_back(-1);
73 std::string::size_type pos = 0;
74 while ((pos = m_line.find(',', pos)) != std::string::npos) {
75 m_data.emplace_back(pos);
76 ++pos;
77 }
78 // This checks for a trailing comma with no data after it.
79 pos = m_line.size();
80 m_data.emplace_back(pos);
81 }
82
83private:
84 std::string m_line;
85 std::vector<int> m_data;
86};
87
88static std::istream& operator>>(std::istream& str, CSVRow& data)
89{
90 data.readNextRow(str);
91 return str;
92}
93
94template <unsigned int BUSWIDTH = DEFAULT_TLM_BUSWIDTH>
95class loader : public sc_core::sc_module
96{
97 SCP_LOGGER();
98 cci::cci_broker_handle m_broker;
99
100 template <typename MODULE, typename TYPES = tlm::tlm_base_protocol_types>
101 class simple_initiator_socket_zero
102 : public tlm_utils::simple_initiator_socket_b<MODULE, BUSWIDTH, TYPES, sc_core::SC_ZERO_OR_MORE_BOUND>
103 {
104 using typename tlm_utils::simple_initiator_socket_b<MODULE, BUSWIDTH, TYPES,
105 sc_core::SC_ZERO_OR_MORE_BOUND>::base_target_socket_type;
106 typedef tlm_utils::simple_initiator_socket_b<MODULE, BUSWIDTH, TYPES, sc_core::SC_ZERO_OR_MORE_BOUND> socket_b;
107
108 public:
109 simple_initiator_socket_zero(const char* name): socket_b(name) {}
110 };
111
112public:
113 simple_initiator_socket_zero<loader<BUSWIDTH>> initiator_socket;
115 onMethodHelper m_onMethod;
116
117private:
118 uint64_t m_address = 0;
119
120 std::function<void(const uint8_t* data, uint64_t offset, uint64_t len)> write_cb;
121 bool m_use_callback = false;
122
123 std::list<std::string> sc_cci_children(sc_core::sc_module_name name)
124 {
125 std::list<std::string> children;
126 const std::string prefix = std::string(name) + ".";
127 const size_t prefix_length = prefix.length();
128 const auto append_child = [&children, prefix_length](const std::string& full_name) {
129 children.push_back(full_name.substr(prefix_length, full_name.find(".", prefix_length) - prefix_length));
130 };
131 const auto& broker = ConfigurableBroker::get_broker_interface(m_broker);
132 if (const auto* configurable_broker = dynamic_cast<const ConfigurableBroker*>(&broker)) {
133 for (const auto& full_name : configurable_broker->get_unconsumed_preset_names_with_prefix(prefix)) {
134 append_child(full_name);
135 }
136 } else {
137 auto uncon = m_broker.get_unconsumed_preset_values(
138 [&prefix, prefix_length](const std::pair<std::string, cci::cci_value>& iv) {
139 return iv.first.compare(0, prefix_length, prefix) == 0;
140 });
141 for (const auto& p : uncon) {
142 append_child(p.first);
143 }
144 }
145 children.sort();
146 children.unique();
147 return children;
148 }
149
150 void send(uint64_t addr, uint8_t* data, uint64_t len)
151 {
152 if (m_use_callback) {
153 write_cb(data, addr, len);
154 } else {
155 tlm::tlm_generic_payload trans;
156
157 trans.set_command(tlm::TLM_WRITE_COMMAND);
158 trans.set_address(addr);
159 trans.set_data_ptr(data);
160 trans.set_data_length(len);
161 trans.set_streaming_width(len);
162 trans.set_byte_enable_length(0);
163 if (initiator_socket->transport_dbg(trans) != len) {
164 SCP_FATAL(()) << name() << " : Error loading data to memory @ "
165 << "0x" << std::hex << addr;
166 }
167 }
168 }
169
170 template <typename T>
171 T cci_get(std::string name)
172 {
173 return gs::cci_get<T>(m_broker, name);
174 }
175
176 uint32_t byte_swap32(uint32_t in)
177 {
178 uint32_t out;
179 out = in & 0xff;
180 in >>= 8;
181 out <<= 8;
182 out |= in & 0xff;
183 in >>= 8;
184 out <<= 8;
185 out |= in & 0xff;
186 in >>= 8;
187 out <<= 8;
188 out |= in & 0xff;
189 in >>= 8;
190 return out;
191 }
192
193public:
194 loader(sc_core::sc_module_name name)
195 : m_broker(cci::cci_get_broker()), initiator_socket("initiator_socket"), reset("reset")
196 {
197 SCP_TRACE(())("default constructor");
198 reset.register_value_changed_cb([&](bool value) { m_onMethod.run([&]() { doreset(value); }); });
199 }
200 void doreset(bool value)
201 {
202 SCP_WARN(())("Reset");
203 if (value) {
204 end_of_elaboration();
205 }
206 }
207 loader(sc_core::sc_module_name name, std::function<void(const uint8_t* data, uint64_t offset, uint64_t len)> _write)
208 : m_broker(cci::cci_get_broker()), initiator_socket("initiator_socket"), reset("reset"), write_cb(_write)
209 {
210 SCP_TRACE(())("constructor with callback");
211 m_use_callback = true;
212 reset.register_value_changed_cb([&](bool value) { m_onMethod.run([&]() { doreset(value); }); });
213 }
214
215 ~loader() {}
216
217protected:
218 void load(std::string name)
219 {
220 bool read = false;
221 if (m_broker.has_preset_value(name + ".elf_file")) {
222 if (m_use_callback) {
223 SCP_WARN(()) << "elf file loading into local memory will interpret addresses "
224 "relative to the memory - probably not what you want?";
225 }
226 std::string file = gs::cci_get<std::string>(m_broker, name + ".elf_file");
227 elf_load(file);
228 read = true;
229 } else {
230 uint64_t addr = 0;
231 if (m_use_callback) {
232 addr = gs::cci_get<uint64_t>(m_broker, name + ".offset");
233 } else {
234 addr = gs::cci_get<uint64_t>(m_broker, name + ".address");
235 }
236 std::string file;
237 if (gs::cci_get<std::string>(m_broker, name + ".bin_file", file)) {
238 uint64_t file_offset = 0, file_data_len = std::numeric_limits<uint64_t>::max();
239 if (!((gs::cci_get<uint64_t>(m_broker, name + ".bin_file_offset", file_offset) &&
240 gs::cci_get<uint64_t>(m_broker, name + ".bin_file_size", file_data_len)))) {
241 file_data_len = std::filesystem::file_size(std::filesystem::path(file));
242 file_offset = 0;
243 }
244 SCP_INFO(())("Loading binary file: {} starting at offset: {:#x} with size: {:#x} to addr: {:#x}", file,
245 file_offset, file_data_len, addr);
246 file_load(file, addr, file_offset, file_data_len);
247 read = true;
248 }
249 if (gs::cci_get<std::string>(m_broker, name + ".zip_archive", file)) {
250 uint64_t file_offset = 0, file_data_len = 0;
251 if (!((gs::cci_get<uint64_t>(m_broker, name + ".archived_file_offset", file_offset) &&
252 gs::cci_get<uint64_t>(m_broker, name + ".archived_file_size", file_data_len)))) {
253 file_data_len = 0;
254 file_offset = 0;
255 }
256 std::string archived_file_name;
257 if (!gs::cci_get<std::string>(m_broker, name + ".archived_file_name", archived_file_name))
258 archived_file_name = "";
259 SCP_INFO(()) << "Loading " << archived_file_name << " from zip file: " << file
260 << " starting at offset: " << file_offset << " to addr: " << addr;
261 zip_file_load(nullptr, file, addr, archived_file_name, file_offset, file_data_len);
262 read = true;
263 }
264 if (gs::cci_get<std::string>(m_broker, name + ".csv_file", file)) {
265 std::string addr_str = gs::cci_get<std::string>(m_broker, name + ".addr_str");
266 std::string val_str = gs::cci_get<std::string>(m_broker, name + ".value_str");
267 bool byte_swap = false;
268 byte_swap = gs::cci_get<bool>(m_broker, name + ".byte_swap", byte_swap);
269 SCP_INFO(())("Loading csv file {}, ({}) to {:#x}", file, (name + ".csv_file"), addr);
270 csv_load(file, addr, addr_str, val_str, byte_swap);
271 read = true;
272 }
273 std::string param;
274 if (gs::cci_get<std::string>(m_broker, name + ".param", param)) {
275 cci::cci_param_typed_handle<std::string> data(m_broker.get_param_handle(param));
276 if (!data.is_valid()) {
277 SCP_FATAL(()) << "Unable to find valid source param '" << param << "' for '" << name << "'";
278 }
279 SCP_INFO(())("Loading string parameter {} to {:#x}", param, addr);
280 str_load(data.get_value(), addr);
281 read = true;
282 }
283 if (m_broker.has_preset_value(name + ".dtb")) {
284 SCP_FATAL(()) << "'" << name << ".dtb' must be a table {base = ..., overlays = {...}}; "
285 << "use 'bin_file' to load a prebuilt DTB";
286 return;
287 }
288 if (sc_cci_children((name + ".dtb").c_str()).size()) {
289 dtb_load(name + ".dtb", addr);
290 read = true;
291 }
292 if (sc_cci_children((name + ".data").c_str()).size()) {
293 bool byte_swap = false;
294 gs::cci_get<bool>(m_broker, name + ".byte_swap", byte_swap);
295 SCP_INFO(())("Loading config data to {:#x}", addr);
296 data_load(name + ".data", addr, byte_swap);
297 read = true;
298 }
299 }
300 if (!read) {
301 SCP_FATAL(()) << "Unknown loader type: '" << name << "'";
302 }
303 }
304 void end_of_elaboration()
305 {
306 int i = 0;
307 auto children = sc_cci_children(name());
308 for (std::string s : children) {
309 if (std::count_if(s.begin(), s.end(), [](unsigned char c) { return std::isdigit(c); })) {
310 load(std::string(name()) + "." + s);
311 i++;
312 }
313 }
314 if (i == 0 && children.size() > 0) {
315 load(name());
316 }
317 }
318
319public:
326 void file_load(std::string& filename, uint64_t addr, uint64_t file_offset = 0,
327 uint64_t file_data_len = std::numeric_limits<uint64_t>::max())
328 {
329 std::ifstream fin(filename, std::ios::in | std::ios::binary);
330
331 if (!fin.good()) {
332 SCP_FATAL(()) << "Memory::load(): error file not found (" << filename << ")";
333 }
334
335 uint64_t rem_len = file_data_len;
336 uint64_t read_len = 0;
337 fin.seekg(file_offset, std::ios::beg);
338
339 std::vector<uint8_t> buffer(BINFILE_READ_CHUNK_SIZE);
340 uint64_t c = 0;
341
342 while (!fin.eof() && (rem_len > 0)) {
343 read_len = std::min(rem_len, static_cast<uint64_t>(BINFILE_READ_CHUNK_SIZE));
344 fin.read(reinterpret_cast<char*>(buffer.data()), read_len);
345 size_t r = fin.gcount();
346 send(addr + c, buffer.data(), r);
347 c += r;
348 rem_len = ((rem_len >= r) ? rem_len - r : 0);
349 }
350
351 fin.close();
352 }
353
354 void zip_file_load(zip_t* p_archive, const std::string& archive_name, uint64_t addr,
355 const std::string& file_name = "", uint64_t file_offset = 0, uint64_t file_data_len = 0,
356 bool is_compressed = false, const std::string& uncompressed_file_name = "")
357 {
358 std::vector<uint8_t> data;
359 if (!is_compressed) {
360 data = gs::zip_extract(p_archive, archive_name, file_name, file_offset, file_data_len);
361 } else {
362 data = gs::zip_extract_nested(p_archive, archive_name, file_name, uncompressed_file_name, file_offset,
363 file_data_len);
364 }
365 SCP_DEBUG(()) << "load data from zip archive " << archive_name << " to addr: 0x" << std::hex << addr
366 << " len: 0x" << std::hex << data.size();
367 send(addr, data.data(), data.size());
368 }
369
370 void csv_load(std::string filename, uint64_t offset, std::string addr_str, std::string value_str, bool byte_swap)
371 {
372 std::ifstream file(filename);
373
374 CSVRow row;
375 if (!(file >> row)) {
376 SCP_FATAL(()) << "Unable to find " << filename;
377 }
378 int addr_i = -1;
379 int value_i = -1;
380 for (std::size_t i = 0; i < row.size(); i++) {
381 if (row[i] == addr_str) {
382 addr_i = i;
383 }
384 if (row[i] == value_str) {
385 value_i = i;
386 }
387 }
388 if (addr_i == -1 || value_i == -1) {
389 SCP_FATAL(()) << "Unable to find " << filename;
390 }
391 std::size_t min_field_count = std::max(addr_i, value_i);
392 while (file >> row) {
393 if (row.size() <= min_field_count) {
394 continue;
395 }
396 const std::string addr = std::string(row[addr_i]);
397 uint64_t value = std::stoll(std::string(row[value_i]), nullptr, 0);
398 if (byte_swap) value = byte_swap32(value);
399 uint64_t addr_p = std::stoll(addr, nullptr, 0) + offset;
400 send(addr_p, (uint8_t*)(&value), sizeof(value));
401 }
402 }
403
404private:
405 void data_load(std::string param, uint64_t addr, bool byte_swap)
406 {
407 for (std::string s : sc_cci_children(param.c_str())) {
408 if (std::count_if(s.begin(), s.end(), [](unsigned char c) { return std::isdigit(c); })) {
409 union {
410 uint32_t d;
411 uint8_t b[sizeof(uint32_t)];
412 } data;
413 data.d = gs::cci_get<uint32_t>(m_broker, param + "." + std::string(s));
414 if (byte_swap) {
415 data.d = byte_swap32(data.d);
416 }
417 send(addr + (stoi(s) * 0x4), &(data.b[0]), 4);
418 } else {
419 SCP_FATAL(()) << "unknown format for parameter load";
420 break;
421 }
422 }
423 }
424
428 void dtb_load(const std::string& param, uint64_t addr)
429 {
430 for (const std::string& child : sc_cci_children(param.c_str())) {
431 if (child != "base" && child != "overlays" && child != "dump") {
432 SCP_FATAL(()) << "'" << param << "': unknown key '" << child << "' (expected base, overlays, dump)";
433 return;
434 }
435 }
436
437 std::string base;
438 if (!m_broker.has_preset_value(param + ".base")) {
439 SCP_FATAL(()) << "'" << param << "' requires a 'base' DTB file";
440 return;
441 }
442 if (!gs::cci_get<std::string>(m_broker, param + ".base", base) || base.empty()) {
443 SCP_FATAL(()) << "'" << param << ".base' must be a file name";
444 return;
445 }
446
447 std::vector<std::string> overlays;
448 const std::string ov_key = param + ".overlays";
449 if (m_broker.has_preset_value(ov_key)) {
450 SCP_FATAL(()) << "'" << ov_key << "' must be a list of file names";
451 return;
452 }
453 // sc_cci_children returns sorted strings ("10" < "2"), so count the
454 // entries and require exactly the indices 0..N-1.
455 const size_t n_overlays = sc_cci_children(ov_key.c_str()).size();
456 for (size_t i = 0; i < n_overlays; ++i) {
457 const std::string key = ov_key + "." + std::to_string(i);
458 std::string overlay;
459 if (!m_broker.has_preset_value(key) || !gs::cci_get<std::string>(m_broker, key, overlay) ||
460 overlay.empty()) {
461 SCP_FATAL(()) << "'" << ov_key << "' must be a list of file names (bad or missing entry " << i + 1
462 << ")";
463 return;
464 }
465 overlays.push_back(overlay);
466 }
467
468 std::string dump;
469 const bool has_dump = m_broker.has_preset_value(param + ".dump");
470 if (has_dump && !gs::cci_get<std::string>(m_broker, param + ".dump", dump)) {
471 SCP_FATAL(()) << "'" << param << ".dump' must be a file name";
472 return;
473 }
474
475 std::string error;
476 std::vector<uint8_t> dtb = gs::dtb_build(base, overlays, error);
477 if (dtb.empty()) {
478 SCP_FATAL(()) << "'" << param << "': " << error;
479 return;
480 }
481
482 if (!dump.empty()) {
483 std::ofstream out(dump, std::ios::binary | std::ios::trunc);
484 out.write(reinterpret_cast<const char*>(dtb.data()), static_cast<std::streamsize>(dtb.size()));
485 out.close();
486 if (out.fail()) {
487 SCP_FATAL(()) << "'" << param << "': unable to write '" << dump << "'";
488 return;
489 }
490 }
491
492 SCP_INFO(())
493 ("Loading DTB {} with {} overlay(s), size {:#x}, to addr: {:#x}", base, overlays.size(), dtb.size(), addr);
494 send(addr, dtb.data(), dtb.size());
495 }
496
497public:
498 void str_load(std::string data, uint64_t addr)
499 {
500 send(addr, reinterpret_cast<uint8_t*>(const_cast<char*>(data.c_str())), data.length());
501 }
502
503 void ptr_load(uint8_t* data, uint64_t addr, uint64_t len) { send(addr, data, len); }
504
505 void elf_load(const std::string& path)
506 {
507 gs::elf_loader(path, [&](uint64_t addr, uint8_t* data, uint64_t len) -> void { send(addr, data, len); });
508 }
509};
510} // namespace gs
511
512extern "C" void module_register();
513#endif
Definition target-signal-socket.h:163
Definition loader.h:60
Configurable Broker class, inherits from the 'standard' cci_utils broker, but adds 1/ The ability to ...
Definition cciutils.h:321
void file_load(std::string &filename, uint64_t addr, uint64_t file_offset=0, uint64_t file_data_len=std::numeric_limits< uint64_t >::max())
This function reads a file into memory and can be used to load an image.
Definition loader.h:326
Definition onmethod.h:14
Tool which reads a Lua configuration file and sets parameters.
Definition biflow.cc:10
std::vector< uint8_t > zip_extract_nested(zip_t *archive, const std::string &archive_name, const std::string &compressed_file_name, const std::string &inner_file_name, uint64_t file_offset=0, uint64_t file_data_len=0)
Extract compressed data from a nested zip archive.
Definition zip_loader.cc:97
std::vector< uint8_t > dtb_build(const std::string &base, const std::vector< std::string > &overlays, std::string &error)
Build a flattened device tree from a base DTB and ordered overlays.
Definition dtb_loader.cc:112
std::vector< uint8_t > zip_extract(zip_t *archive, const std::string &archive_name, const std::string &file_name="", uint64_t file_offset=0, uint64_t file_data_len=0)
Extract data from a zip archive.
Definition zip_loader.cc:57