| 1 |
811 |
skrzyp |
//==========================================================================
|
| 2 |
|
|
//
|
| 3 |
|
|
// redboot_linux_boot.c
|
| 4 |
|
|
//
|
| 5 |
|
|
// RedBoot command to boot Linux on ARM platforms
|
| 6 |
|
|
//
|
| 7 |
|
|
//==========================================================================
|
| 8 |
|
|
// ####ECOSGPLCOPYRIGHTBEGIN####
|
| 9 |
|
|
// -------------------------------------------
|
| 10 |
|
|
// This file is part of eCos, the Embedded Configurable Operating System.
|
| 11 |
|
|
// Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
|
| 12 |
|
|
//
|
| 13 |
|
|
// eCos is free software; you can redistribute it and/or modify it under
|
| 14 |
|
|
// the terms of the GNU General Public License as published by the Free
|
| 15 |
|
|
// Software Foundation; either version 2 or (at your option) any later
|
| 16 |
|
|
// version.
|
| 17 |
|
|
//
|
| 18 |
|
|
// eCos is distributed in the hope that it will be useful, but WITHOUT
|
| 19 |
|
|
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
| 20 |
|
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
| 21 |
|
|
// for more details.
|
| 22 |
|
|
//
|
| 23 |
|
|
// You should have received a copy of the GNU General Public License
|
| 24 |
|
|
// along with eCos; if not, write to the Free Software Foundation, Inc.,
|
| 25 |
|
|
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
| 26 |
|
|
//
|
| 27 |
|
|
// As a special exception, if other files instantiate templates or use
|
| 28 |
|
|
// macros or inline functions from this file, or you compile this file
|
| 29 |
|
|
// and link it with other works to produce a work based on this file,
|
| 30 |
|
|
// this file does not by itself cause the resulting work to be covered by
|
| 31 |
|
|
// the GNU General Public License. However the source code for this file
|
| 32 |
|
|
// must still be made available in accordance with section (3) of the GNU
|
| 33 |
|
|
// General Public License v2.
|
| 34 |
|
|
//
|
| 35 |
|
|
// This exception does not invalidate any other reasons why a work based
|
| 36 |
|
|
// on this file might be covered by the GNU General Public License.
|
| 37 |
|
|
// -------------------------------------------
|
| 38 |
|
|
// ####ECOSGPLCOPYRIGHTEND####
|
| 39 |
|
|
//####OTHERCOPYRIGHTBEGIN####
|
| 40 |
|
|
//
|
| 41 |
|
|
// The structure definitions below are taken from include/asm-arm/setup.h in
|
| 42 |
|
|
// the Linux kernel, Copyright (C) 1997-1999 Russell King. Their presence
|
| 43 |
|
|
// here is for the express purpose of communication with the Linux kernel
|
| 44 |
|
|
// being booted and is considered 'fair use' by the original author and
|
| 45 |
|
|
// are included with his permission.
|
| 46 |
|
|
//
|
| 47 |
|
|
//####OTHERCOPYRIGHTEND####
|
| 48 |
|
|
//==========================================================================
|
| 49 |
|
|
//#####DESCRIPTIONBEGIN####
|
| 50 |
|
|
//
|
| 51 |
|
|
// Author(s): Piotr Skrzypek
|
| 52 |
838 |
skrzyp |
// Contributor: R.Diez
|
| 53 |
811 |
skrzyp |
// Date: 2012-06-08
|
| 54 |
|
|
// Purpose:
|
| 55 |
|
|
// Description: Based on ARM code
|
| 56 |
|
|
//
|
| 57 |
|
|
// This code is part of RedBoot (tm).
|
| 58 |
|
|
//
|
| 59 |
|
|
//####DESCRIPTIONEND####
|
| 60 |
|
|
//
|
| 61 |
|
|
//==========================================================================
|
| 62 |
|
|
|
| 63 |
|
|
#include <pkgconf/hal.h>
|
| 64 |
|
|
#include <redboot.h>
|
| 65 |
|
|
#include <cyg/hal/hal_intr.h>
|
| 66 |
|
|
#include <cyg/hal/hal_cache.h>
|
| 67 |
|
|
#include <cyg/hal/hal_io.h>
|
| 68 |
|
|
|
| 69 |
|
|
#ifdef CYGPKG_IO_ETH_DRIVERS
|
| 70 |
|
|
#include <cyg/io/eth/eth_drv.h>
|
| 71 |
|
|
#endif
|
| 72 |
|
|
|
| 73 |
|
|
// Exported CLI function(s)
|
| 74 |
|
|
static void do_exec(int argc, char *argv[]);
|
| 75 |
|
|
RedBoot_cmd("exec",
|
| 76 |
|
|
"Execute an image",
|
| 77 |
|
|
"[-w timeout] [-b <load addr> [-l <length>]]\n"
|
| 78 |
|
|
" [-t <target> ] [<entry_point>]",
|
| 79 |
|
|
do_exec
|
| 80 |
|
|
);
|
| 81 |
|
|
|
| 82 |
|
|
static void do_exec(int argc, char *argv[]) {
|
| 83 |
|
|
|
| 84 |
|
|
cyg_uint32 wait_time;
|
| 85 |
|
|
cyg_bool wait_time_set;
|
| 86 |
|
|
cyg_uint32 base_addr;
|
| 87 |
|
|
cyg_bool base_addr_set;
|
| 88 |
|
|
cyg_uint32 length;
|
| 89 |
|
|
cyg_bool length_set;
|
| 90 |
|
|
cyg_uint32 target;
|
| 91 |
|
|
cyg_bool target_set;
|
| 92 |
|
|
cyg_uint32 entry;
|
| 93 |
|
|
|
| 94 |
|
|
extern char __tramp_start__[], __tramp_end__[];
|
| 95 |
|
|
cyg_uint32 oldints;
|
| 96 |
|
|
|
| 97 |
|
|
// Parse parameters
|
| 98 |
|
|
struct option_info opts[4];
|
| 99 |
|
|
init_opts(&opts[0], 'w', true, OPTION_ARG_TYPE_NUM, (void **)&wait_time, (bool *)&wait_time_set, "wait timeout");
|
| 100 |
|
|
init_opts(&opts[1], 'b', true, OPTION_ARG_TYPE_NUM, (void **)&base_addr, (bool *)&base_addr_set, "base address");
|
| 101 |
|
|
init_opts(&opts[2], 'l', true, OPTION_ARG_TYPE_NUM, (void **)&length, (bool *)&length_set, "length");
|
| 102 |
|
|
init_opts(&opts[3], 't', true, OPTION_ARG_TYPE_NUM, (void **)&target, (bool *)&target_set, "target address");
|
| 103 |
|
|
int num_opts = 4;
|
| 104 |
|
|
|
| 105 |
|
|
if(!scan_opts(argc, argv, 1, opts, num_opts, (void *)&entry, OPTION_ARG_TYPE_NUM, "starting address")) {
|
| 106 |
|
|
return;
|
| 107 |
|
|
}
|
| 108 |
|
|
|
| 109 |
|
|
// Timeout is optional
|
| 110 |
|
|
wait_time = wait_time_set ? wait_time : 0;
|
| 111 |
|
|
|
| 112 |
|
|
// Base address is mandatory
|
| 113 |
|
|
if(!base_addr_set) {
|
| 114 |
|
|
diag_printf("Use -b option to provide base address\n");
|
| 115 |
|
|
return;
|
| 116 |
|
|
}
|
| 117 |
|
|
|
| 118 |
|
|
// Length is mandatory
|
| 119 |
|
|
if(!length_set) {
|
| 120 |
|
|
diag_printf("Use -l option to provide size of the binary\n");
|
| 121 |
|
|
return;
|
| 122 |
|
|
}
|
| 123 |
|
|
length = (length + 3) & ~0x03;
|
| 124 |
|
|
|
| 125 |
|
|
// Target address is mandatory
|
| 126 |
|
|
if(!target_set) {
|
| 127 |
|
|
diag_printf("Use -t option to provide target address\n");
|
| 128 |
|
|
return;
|
| 129 |
|
|
}
|
| 130 |
|
|
|
| 131 |
|
|
// Optionally wait
|
| 132 |
|
|
if(wait_time_set) {
|
| 133 |
|
|
int script_timeout_ms = wait_time * 1000;
|
| 134 |
|
|
#ifdef CYGFUN_REDBOOT_BOOT_SCRIPT
|
| 135 |
|
|
unsigned char *hold_script = script;
|
| 136 |
|
|
script = (unsigned char *) 0;
|
| 137 |
|
|
#endif
|
| 138 |
|
|
diag_printf("About to start execution of image at %p, entry point %p - abort with ^C within %d seconds\n",
|
| 139 |
|
|
(void *)target, (void *)entry, wait_time);
|
| 140 |
|
|
while(script_timeout_ms >= CYGNUM_REDBOOT_CLI_IDLE_TIMEOUT) {
|
| 141 |
|
|
char line[8];
|
| 142 |
|
|
int res = _rb_gets(line, sizeof(line), CYGNUM_REDBOOT_CLI_IDLE_TIMEOUT);
|
| 143 |
|
|
if(res == _GETS_CTRLC) {
|
| 144 |
|
|
#ifdef CYGFUN_REDBOOT_BOOT_SCRIPT
|
| 145 |
|
|
script = hold_script;
|
| 146 |
|
|
#endif
|
| 147 |
|
|
return;
|
| 148 |
|
|
}
|
| 149 |
|
|
script_timeout_ms -= CYGNUM_REDBOOT_CLI_IDLE_TIMEOUT;
|
| 150 |
|
|
}
|
| 151 |
|
|
}
|
| 152 |
|
|
|
| 153 |
|
|
#ifdef CYGPKG_IO_ETH_DRIVERS
|
| 154 |
|
|
eth_drv_stop();
|
| 155 |
|
|
#endif
|
| 156 |
|
|
|
| 157 |
|
|
HAL_DISABLE_INTERRUPTS(oldints);
|
| 158 |
|
|
HAL_DCACHE_SYNC();
|
| 159 |
|
|
HAL_ICACHE_DISABLE();
|
| 160 |
|
|
HAL_DCACHE_DISABLE();
|
| 161 |
|
|
HAL_DCACHE_SYNC();
|
| 162 |
|
|
HAL_ICACHE_INVALIDATE_ALL();
|
| 163 |
|
|
HAL_DCACHE_INVALIDATE_ALL();
|
| 164 |
|
|
|
| 165 |
|
|
// Copy the trampoline code below
|
| 166 |
|
|
memcpy((char*)CYGPKG_REDBOOT_OPENRISC_TRAMPOLINE_ADDRESS, __tramp_start__, __tramp_end__ - __tramp_start__);
|
| 167 |
|
|
|
| 168 |
|
|
// Jump to the trampoline. Pass arguments according to ABI
|
| 169 |
|
|
void (*trampoline)(cyg_uint32 bin_start, cyg_uint32 bin_end, cyg_uint32 dest_base, cyg_uint32 entry);
|
| 170 |
|
|
trampoline = CYGPKG_REDBOOT_OPENRISC_TRAMPOLINE_ADDRESS;
|
| 171 |
|
|
trampoline(base_addr, base_addr + length, target, entry);
|
| 172 |
|
|
|
| 173 |
|
|
// This code is executed from the trampoline address. Trampoline never returns.
|
| 174 |
|
|
asm volatile (
|
| 175 |
838 |
skrzyp |
|
| 176 |
|
|
// All code below must be position independent,
|
| 177 |
|
|
// as it will run on another memory address.
|
| 178 |
|
|
// Accorting to ABI:
|
| 179 |
|
|
// R3 is base_addr
|
| 180 |
|
|
// R4 is base_addr + length
|
| 181 |
|
|
// R5 is target
|
| 182 |
|
|
// R6 is the entry point to jump to at the end
|
| 183 |
|
|
|
| 184 |
811 |
skrzyp |
"__tramp_start__:\n"
|
| 185 |
|
|
"1: l.sfeq r3, r4\n"
|
| 186 |
|
|
" l.bf 2f\n"
|
| 187 |
|
|
" l.lwz r13,0x00(r3)\n"
|
| 188 |
|
|
" l.sw 0x00(r5), r13\n"
|
| 189 |
|
|
" l.addi r3, r3, 4\n"
|
| 190 |
838 |
skrzyp |
#ifdef CYGHWR_BRANCH_SLOT_IMPLEMENTED
|
| 191 |
811 |
skrzyp |
" l.j 1b\n"
|
| 192 |
|
|
" l.addi r5, r5, 4\n"
|
| 193 |
838 |
skrzyp |
#else
|
| 194 |
|
|
" l.addi r5, r5, 4\n"
|
| 195 |
|
|
" l.j 1b\n"
|
| 196 |
|
|
#endif
|
| 197 |
811 |
skrzyp |
"2: l.jr r6\n"
|
| 198 |
838 |
skrzyp |
#ifdef CYGHWR_BRANCH_SLOT_IMPLEMENTED
|
| 199 |
811 |
skrzyp |
" l.nop\n"
|
| 200 |
838 |
skrzyp |
#endif
|
| 201 |
811 |
skrzyp |
"__tramp_end__:\n"
|
| 202 |
|
|
: /* no output registers */
|
| 203 |
|
|
: /* no input registers */
|
| 204 |
|
|
: /* no clobbered registers */
|
| 205 |
|
|
);
|
| 206 |
|
|
|
| 207 |
|
|
|
| 208 |
|
|
}
|
| 209 |
|
|
|
| 210 |
|
|
// EOF redboot_linux_exec.c
|