| 1 | // ========== Copyright Header Begin ========================================== |
| 2 | // |
| 3 | // OpenSPARC T2 Processor File: ui_mem_cmds.cc |
| 4 | // Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved. |
| 5 | // DO NOT ALTER OR REMOVE COPYRIGHT NOTICES. |
| 6 | // |
| 7 | // The above named program is free software; you can redistribute it and/or |
| 8 | // modify it under the terms of the GNU General Public |
| 9 | // License version 2 as published by the Free Software Foundation. |
| 10 | // |
| 11 | // The above named program is distributed in the hope that it will be |
| 12 | // useful, but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | // General Public License for more details. |
| 15 | // |
| 16 | // You should have received a copy of the GNU General Public |
| 17 | // License along with this work; if not, write to the Free Software |
| 18 | // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. |
| 19 | // |
| 20 | // ========== Copyright Header End ============================================ |
| 21 | /* |
| 22 | * Copyright (C) 2001 Sun Microsystems, Inc. |
| 23 | * All rights reserved. |
| 24 | */ |
| 25 | #pragma ident "@(#)1.19 07/11/19 ui_mem_cmds.cc" |
| 26 | |
| 27 | #include <sys/types.h> |
| 28 | #include <sys/mman.h> |
| 29 | #include <assert.h> |
| 30 | #include <fcntl.h> |
| 31 | #include <stdio.h> |
| 32 | #include <stdlib.h> |
| 33 | #include <strings.h> |
| 34 | #include <unistd.h> |
| 35 | #include <libelf.h> |
| 36 | #include <sys/dkio.h> |
| 37 | #include <sys/dklabel.h> |
| 38 | #include <sys/vtoc.h> |
| 39 | |
| 40 | |
| 41 | #include "types.h" |
| 42 | #include "mem.h" |
| 43 | |
| 44 | #include "system.h" |
| 45 | #include "dr.h" |
| 46 | #include "ui.h" |
| 47 | |
| 48 | typedef void* TM_OPAQUE_DATA; |
| 49 | #include "tracemod.h" |
| 50 | |
| 51 | #include "blaze_globals.h" |
| 52 | #include "ui_utils.h" |
| 53 | #include "fileutil.h" |
| 54 | |
| 55 | /////////////////////////////////////////////////////////////////////// |
| 56 | |
| 57 | void load_elf64(char *filename, uint64_t base_pa, uint64_t base_va); |
| 58 | static void load_elf64_section(Elf64_Shdr *shdr, Elf_Scn *scn, |
| 59 | uint64_t base_pa, uint64_t base_va); |
| 60 | |
| 61 | /////////////////////////////////////////////////////////////////////// |
| 62 | |
| 63 | extern bool_t schizo_rom_match (uint64_t paddr); |
| 64 | extern bool_t schizo_rom_store (uint32_t cpu_id, uint64_t paddr, uint32_t size, uint64_t *buf); |
| 65 | |
| 66 | /////////////////////////////////////////////////////////////////////// |
| 67 | |
| 68 | int load_cmd(char *cp, void *) |
| 69 | { |
| 70 | char *filename; |
| 71 | char *token; |
| 72 | uint64_t base_pa; |
| 73 | uint64_t base_va; |
| 74 | |
| 75 | cp = ui_parsew(cp, &token); |
| 76 | base_pa = strtoll(token, (char**)NULL, 16); |
| 77 | |
| 78 | cp = ui_parsew(cp, &filename); |
| 79 | cp = ui_parsew(cp, &token); |
| 80 | base_va = strtoll(token, (char**)NULL, 16); |
| 81 | |
| 82 | load_elf64(filename, base_pa, base_va); |
| 83 | |
| 84 | return UI_CMD_DONE; |
| 85 | } |
| 86 | |
| 87 | void load_elf64(char *filename, uint64_t base_pa, uint64_t base_va) |
| 88 | { |
| 89 | int fd; |
| 90 | Elf *elf; |
| 91 | Elf64_Ehdr *ehdr; |
| 92 | Elf_Scn *scn; |
| 93 | Elf64_Shdr *shdr; |
| 94 | Elf_Data *data; |
| 95 | u_int cnt; |
| 96 | |
| 97 | /* Open the input file */ |
| 98 | fd = SYSTEM_open(filename, O_RDONLY); |
| 99 | if (fd == -1) { |
| 100 | ui->error("<%s> not found \n", filename); |
| 101 | exit (0); |
| 102 | } |
| 103 | |
| 104 | |
| 105 | /* Obtain the ELF descriptor */ |
| 106 | (void) elf_version(EV_CURRENT); |
| 107 | if ((elf = elf_begin(fd, ELF_C_READ, NULL)) == NULL) { |
| 108 | ui->error("%s\n", elf_errmsg(elf_errno())); |
| 109 | exit(0); |
| 110 | } |
| 111 | |
| 112 | /* Obtain the .shstrtab data buffer */ |
| 113 | if (((ehdr = elf64_getehdr(elf)) == NULL) || |
| 114 | ((scn = elf_getscn(elf, ehdr->e_shstrndx)) == NULL) || |
| 115 | ((data = elf_getdata(scn, NULL)) == NULL)) { |
| 116 | ui->error("%s\n", elf_errmsg(elf_errno())); |
| 117 | exit(0); |
| 118 | } |
| 119 | |
| 120 | /* Traverse input filename, looking for relevant sections */ |
| 121 | for (cnt = 1, scn = NULL; scn = elf_nextscn(elf, scn); cnt++) { |
| 122 | if ((shdr = elf64_getshdr(scn)) == NULL) { |
| 123 | ui->error("%s\n", elf_errmsg(elf_errno())); |
| 124 | exit(0); |
| 125 | } |
| 126 | if (strcmp((char *)data->d_buf+shdr->sh_name, |
| 127 | ".text") == 0) { |
| 128 | load_elf64_section(shdr, scn, base_pa, base_va); |
| 129 | } else if (strcmp((char *)data->d_buf+shdr->sh_name, |
| 130 | ".rodata") == 0) { |
| 131 | load_elf64_section(shdr, scn, base_pa, base_va); |
| 132 | } else if (strcmp((char *)data->d_buf+shdr->sh_name, |
| 133 | ".rodata1") == 0) { |
| 134 | load_elf64_section(shdr, scn, base_pa, base_va); |
| 135 | } else if (strcmp((char *)data->d_buf+shdr->sh_name, |
| 136 | ".data") == 0) { |
| 137 | load_elf64_section(shdr, scn, base_pa, base_va); |
| 138 | } else if (strcmp((char *)data->d_buf+shdr->sh_name, |
| 139 | ".data1") == 0) { |
| 140 | load_elf64_section(shdr, scn, base_pa, base_va); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | elf_end(elf); |
| 145 | close(fd); |
| 146 | } |
| 147 | |
| 148 | /////////////////////////////////////////////////////////////// |
| 149 | |
| 150 | static void load_elf64_section(Elf64_Shdr *shdr, Elf_Scn *scn, |
| 151 | uint64_t base_pa, uint64_t base_va) |
| 152 | { |
| 153 | uint64_t offset; |
| 154 | Elf_Data *data; |
| 155 | |
| 156 | if ((data = elf_getdata(scn, NULL)) == NULL) { |
| 157 | ui->error("%s\n", elf_errmsg(elf_errno())); |
| 158 | exit(0); |
| 159 | } |
| 160 | |
| 161 | offset = shdr->sh_addr - base_va; |
| 162 | |
| 163 | memwrite8u_blk_nl(mm1, base_pa + offset, (const unsigned char *)data->d_buf, |
| 164 | data->d_size); |
| 165 | } |
| 166 | |
| 167 | /////////////////////////////////////////////////////////////// |
| 168 | |
| 169 | |
| 170 | |
| 171 | #ifdef NIAGARA |
| 172 | |
| 173 | bool_t load_mem_image(const char * file) |
| 174 | { |
| 175 | FILE * fp = fopen (file, "r"); |
| 176 | if (fp == NULL) { |
| 177 | ui->perror(file); |
| 178 | return FALSE; |
| 179 | } |
| 180 | |
| 181 | char s[512]; |
| 182 | uint64_t pa = 0; |
| 183 | |
| 184 | while(fgets(s, 512, fp) != NULL) { |
| 185 | if (s[0] == '@') { |
| 186 | pa = strtoull(s+1, NULL, 16); |
| 187 | continue; |
| 188 | } |
| 189 | |
| 190 | char * last_s; |
| 191 | char * token = strtok_r(s, " \t\n", &last_s); |
| 192 | if (token == NULL) continue; |
| 193 | do { |
| 194 | uint64_t v = strtoull(token, NULL, 16); |
| 195 | if (schizo_rom_match(pa)) { |
| 196 | schizo_rom_store (0, pa, 8, &v); |
| 197 | } else { |
| 198 | memwrite64u_nl(mm1, pa, v); |
| 199 | } |
| 200 | pa += 8; |
| 201 | token = strtok_r(NULL, " \t\n", &last_s); |
| 202 | } while(token != NULL); |
| 203 | } |
| 204 | return true; |
| 205 | |
| 206 | } // bool_t load_mem_image(char * file) |
| 207 | |
| 208 | bool_t load_bin_image (char *file, uint64_t addr, uint64_t size, uint64_t seg_size, int zero_pad, int create_lbl) |
| 209 | { |
| 210 | bool_t is_ni_rom = FALSE; |
| 211 | uint64_t tmppa, buf=0; |
| 212 | |
| 213 | int bytes_read =0; |
| 214 | |
| 215 | is_ni_rom = schizo_rom_match (addr); |
| 216 | if (is_ni_rom) { // If it is rom, it is special |
| 217 | ui->output("ni rom: addr = 0x%llx, size = 0x%llx, seg_size = 0x%llx\n", addr, size, seg_size); |
| 218 | buf = 0; |
| 219 | tmppa = addr; |
| 220 | for (int i= 0; i<seg_size/8; i++) { |
| 221 | schizo_rom_store (0, tmppa, 8, &buf); |
| 222 | tmppa +=8; |
| 223 | } |
| 224 | |
| 225 | FILE * Ffp = fopen (file, "r"); |
| 226 | |
| 227 | for (int i=0; i< size/8; i++) { |
| 228 | bytes_read += fread (&buf, sizeof(uint64_t), 1, Ffp); |
| 229 | schizo_rom_store (0, addr, 8, &buf); |
| 230 | addr +=8; |
| 231 | } |
| 232 | buf = 0; |
| 233 | while (size - bytes_read++) { |
| 234 | schizo_rom_store (0, addr++, 1, &buf); |
| 235 | } |
| 236 | ui->output("total bytes to rom_store 0x%x\n", bytes_read); |
| 237 | } else /* not ni rom */ { |
| 238 | |
| 239 | FILE * fp = fopen (file, "r"); |
| 240 | char s[512]; // char *ps; |
| 241 | uint64_t pa, buf, last_pa = 0; |
| 242 | uint8_t *buf8; |
| 243 | int ret_size = 0; |
| 244 | int bytes_read =0; |
| 245 | AddrPair *ap = NULL; |
| 246 | memT *msp; |
| 247 | |
| 248 | if (fp == NULL) { |
| 249 | ui->perror (file); |
| 250 | return FALSE; |
| 251 | } |
| 252 | buf8 = (uint8_t *)malloc(size); |
| 253 | assert(buf8); |
| 254 | bytes_read += fread(buf8, sizeof(uint8_t), size, fp); |
| 255 | memwrite8u_blk_nl(mm1, addr, (const unsigned char *)buf8, (int)size); |
| 256 | |
| 257 | if (create_lbl) { |
| 258 | ui->error("create_lbl not supported in this version. Please use v4.49 or prior\n"); |
| 259 | exit(1); |
| 260 | } |
| 261 | } // ni-rom or phys RAM? |
| 262 | } |
| 263 | |
| 264 | #endif //// NIAGARA |