/* * VAS_EBOOT -- GRand Unified Bootloader * Copyright (C) 2009 Free Software Foundation, Inc. * * VAS_EBOOT is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * VAS_EBOOT is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with VAS_EBOOT. If not, see . */ #include #include #include #include #include #include #include #include /* Do we need mips64? */ extern VasEBoot_uint8_t VasEBoot_relocator_forward_start; extern VasEBoot_uint8_t VasEBoot_relocator_forward_end; extern VasEBoot_uint8_t VasEBoot_relocator_backward_start; extern VasEBoot_uint8_t VasEBoot_relocator_backward_end; #define REGW_SIZEOF (2 * sizeof (VasEBoot_uint32_t)) #define JUMP_SIZEOF (2 * sizeof (VasEBoot_uint32_t)) #define RELOCATOR_SRC_SIZEOF(x) (&VasEBoot_relocator_##x##_end \ - &VasEBoot_relocator_##x##_start) #define RELOCATOR_SIZEOF(x) (RELOCATOR_SRC_SIZEOF(x) \ + REGW_SIZEOF * 3) VasEBoot_size_t VasEBoot_relocator_align = sizeof (VasEBoot_uint32_t); VasEBoot_size_t VasEBoot_relocator_forward_size; VasEBoot_size_t VasEBoot_relocator_backward_size; VasEBoot_size_t VasEBoot_relocator_jumper_size = JUMP_SIZEOF + REGW_SIZEOF; VasEBoot_size_t VasEBoot_relocator_preamble_size = 0; void VasEBoot_cpu_relocator_init (void) { VasEBoot_relocator_forward_size = RELOCATOR_SIZEOF(forward); VasEBoot_relocator_backward_size = RELOCATOR_SIZEOF(backward); } void VasEBoot_cpu_relocator_preamble (void *rels __attribute__ ((unused))) { } static void write_reg (int regn, VasEBoot_uint32_t val, void **target) { /* lui $r, (val+0x8000). */ *(VasEBoot_uint32_t *) *target = ((0x3c00 | regn) << 16) | ((val + 0x8000) >> 16); *target = ((VasEBoot_uint32_t *) *target) + 1; /* addiu $r, $r, val. */ *(VasEBoot_uint32_t *) *target = (((0x2400 | regn << 5 | regn) << 16) | (val & 0xffff)); *target = ((VasEBoot_uint32_t *) *target) + 1; } static void write_jump (int regn, void **target) { /* j $r. */ *(VasEBoot_uint32_t *) *target = (regn<<21) | 0x8; *target = ((VasEBoot_uint32_t *) *target) + 1; /* nop. */ *(VasEBoot_uint32_t *) *target = 0; *target = ((VasEBoot_uint32_t *) *target) + 1; } void VasEBoot_cpu_relocator_jumper (void *rels, VasEBoot_addr_t addr) { write_reg (1, addr, &rels); write_jump (1, &rels); } void VasEBoot_cpu_relocator_backward (void *ptr0, void *src, void *dest, VasEBoot_size_t size) { void *ptr = ptr0; write_reg (8, (VasEBoot_uint32_t) src, &ptr); write_reg (9, (VasEBoot_uint32_t) dest, &ptr); write_reg (10, (VasEBoot_uint32_t) size, &ptr); VasEBoot_memcpy (ptr, &VasEBoot_relocator_backward_start, RELOCATOR_SRC_SIZEOF (backward)); } void VasEBoot_cpu_relocator_forward (void *ptr0, void *src, void *dest, VasEBoot_size_t size) { void *ptr = ptr0; write_reg (8, (VasEBoot_uint32_t) src, &ptr); write_reg (9, (VasEBoot_uint32_t) dest, &ptr); write_reg (10, (VasEBoot_uint32_t) size, &ptr); VasEBoot_memcpy (ptr, &VasEBoot_relocator_forward_start, RELOCATOR_SRC_SIZEOF (forward)); } VasEBoot_err_t VasEBoot_relocator32_boot (struct VasEBoot_relocator *rel, struct VasEBoot_relocator32_state state) { VasEBoot_relocator_chunk_t ch; void *ptr; VasEBoot_err_t err; void *relst; VasEBoot_size_t relsize; VasEBoot_size_t stateset_size = 31 * REGW_SIZEOF + JUMP_SIZEOF; unsigned i; VasEBoot_addr_t vtarget; err = VasEBoot_relocator_alloc_chunk_align (rel, &ch, 0, UP_TO_TOP32 (stateset_size), stateset_size, sizeof (VasEBoot_uint32_t), VAS_EBOOT_RELOCATOR_PREFERENCE_NONE, 0); if (err) return err; ptr = get_virtual_current_address (ch); for (i = 1; i < 32; i++) write_reg (i, state.gpr[i], &ptr); write_jump (state.jumpreg, &ptr); vtarget = (VasEBoot_addr_t) VasEBoot_map_memory (get_physical_target_address (ch), stateset_size); err = VasEBoot_relocator_prepare_relocs (rel, vtarget, &relst, &relsize); if (err) return err; VasEBoot_arch_sync_caches ((void *) relst, relsize); ((void (*) (void)) relst) (); /* Not reached. */ return VAS_EBOOT_ERR_NONE; }