OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-2.0/] [packages/] [hal/] [arm/] [arch/] [v2_0/] [src/] [redboot_linux_exec.c] - Blame information for rev 587

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 27 unneback
//==========================================================================
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 Red Hat, Inc.
12
// Copyright (C) 2003 Gary Thomas
13
//
14
// eCos is free software; you can redistribute it and/or modify it under
15
// the terms of the GNU General Public License as published by the Free
16
// Software Foundation; either version 2 or (at your option) any later version.
17
//
18
// eCos is distributed in the hope that it will be useful, but WITHOUT ANY
19
// 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 along
24
// with eCos; if not, write to the Free Software Foundation, Inc.,
25
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
26
//
27
// As a special exception, if other files instantiate templates or use macros
28
// or inline functions from this file, or you compile this file and link it
29
// with other works to produce a work based on this file, this file does not
30
// by itself cause the resulting work to be covered by the GNU General Public
31
// License. However the source code for this file must still be made available
32
// in accordance with section (3) of the GNU General Public License.
33
//
34
// This exception does not invalidate any other reasons why a work based on
35
// this file might be covered by the GNU General Public License.
36
//
37
// Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
38
// at http://sources.redhat.com/ecos/ecos-license/
39
// -------------------------------------------
40
//####ECOSGPLCOPYRIGHTEND####
41
//####OTHERCOPYRIGHTBEGIN####
42
//
43
//  The structure definitions below are taken from include/asm-arm/setup.h in
44
//  the Linux kernel, Copyright (C) 1997-1999 Russell King. Their presence
45
//  here is for the express purpose of communication with the Linux kernel
46
//  being booted and is considered 'fair use' by the original author and
47
//  are included with his permission.
48
//
49
//####OTHERCOPYRIGHTEND####
50
//==========================================================================
51
//#####DESCRIPTIONBEGIN####
52
//
53
// Author(s):    gthomas
54
// Contributors: gthomas, jskov,
55
//               Russell King <rmk@arm.linux.org.uk>
56
// Date:         2001-02-20
57
// Purpose:      
58
// Description:  
59
//              
60
// This code is part of RedBoot (tm).
61
//
62
//####DESCRIPTIONEND####
63
//
64
//==========================================================================
65
 
66
#include <pkgconf/hal.h>
67
#include <redboot.h>
68
 
69
#include <cyg/hal/hal_intr.h>
70
#include <cyg/hal/hal_cache.h>
71
#include CYGHWR_MEMORY_LAYOUT_H
72
 
73
#include <cyg/hal/hal_io.h>
74
 
75
#ifndef CYGARC_PHYSICAL_ADDRESS
76
# error
77
# define CYGARC_PHYSICAL_ADDRESS(x) (x)
78
#endif
79
 
80
// FIXME: This should be a CDL variable, and CYGSEM_REDBOOT_ARM_LINUX_BOOT
81
//        active_if  CYGHWR_HAL_ARM_REDBOOT_MACHINE_TYPE>0
82
#ifdef HAL_PLATFORM_MACHINE_TYPE
83
#define CYGHWR_REDBOOT_ARM_MACHINE_TYPE HAL_PLATFORM_MACHINE_TYPE
84
 
85
// Exported CLI function(s)
86
static void do_exec(int argc, char *argv[]);
87
RedBoot_cmd("exec",
88
            "Execute an image - with MMU off",
89
            "[-w timeout] [-b <load addr> [-l <length>]]\n"
90
            "        [-r <ramdisk addr> [-s <ramdisk length>]]\n"
91
            "        [-c \"kernel command line\"] [<entry_point>]",
92
            do_exec
93
    );
94
 
95
// CYGARC_HAL_MMU_OFF inserts code to turn off MMU and jump to a physical
96
// address. Some ARM implementations may need special handling and define
97
// their own version.
98
#ifndef CYGARC_HAL_MMU_OFF
99
#define CYGARC_HAL_MMU_OFF(__paddr__) \
100
  "   mcr p15,0,r0,c7,c10,4\n"        \
101
  "   mcr p15,0,r0,c7,c7,0\n"         \
102
  "   mrc p15,0,r0,c1,c0,0\n"         \
103
  "   bic r0,r0,#0xd\n"               \
104
  "   bic r0,r0,#0x1000\n"            \
105
  "   mcr p15,0,r0,c1,c0,0\n"         \
106
  "   mov pc," #__paddr__ "\n"
107
#endif
108
 
109
//
110
// Parameter info for Linux kernel
111
//   ** C A U T I O N **  This setup must match "asm-arm/setup.h"
112
//
113
// Info is passed at a fixed location, using a sequence of tagged
114
// data entries.
115
//
116
 
117
typedef unsigned long  u32;
118
typedef unsigned short u16;
119
typedef unsigned char  u8;
120
 
121
//=========================================================================
122
//  From Linux <asm-arm/setup.h>
123
 
124
#define ATAG_NONE       0x00000000
125
struct tag_header {
126
    u32 size;    // Size of tag (hdr+data) in *longwords*
127
    u32 tag;
128
};
129
 
130
#define ATAG_CORE       0x54410001
131
struct tag_core {
132
    u32 flags;          /* bit 0 = read-only */
133
    u32 pagesize;
134
    u32 rootdev;
135
};
136
 
137
#define ATAG_MEM                0x54410002
138
struct tag_mem32 {
139
    u32 size;
140
    u32 start;
141
};
142
 
143
#define ATAG_VIDEOTEXT  0x54410003
144
struct tag_videotext {
145
    u8   x;
146
    u8   y;
147
    u16  video_page;
148
    u8   video_mode;
149
    u8   video_cols;
150
    u16  video_ega_bx;
151
    u8   video_lines;
152
    u8   video_isvga;
153
    u16  video_points;
154
};
155
 
156
#define ATAG_RAMDISK    0x54410004
157
struct tag_ramdisk {
158
    u32 flags;          /* b0 = load, b1 = prompt */
159
    u32 size;
160
    u32 start;
161
};
162
 
163
/*
164
 * this one accidentally used virtual addresses - as such,
165
 * its deprecated.
166
 */
167
#define ATAG_INITRD     0x54410005
168
 
169
/* describes where the compressed ramdisk image lives (physical address) */
170
#define ATAG_INITRD2    0x54420005
171
struct tag_initrd {
172
    u32 start;
173
    u32 size;
174
};
175
 
176
#define ATAG_SERIAL     0x54410006
177
struct tag_serialnr {
178
    u32 low;
179
    u32 high;
180
};
181
 
182
#define ATAG_REVISION   0x54410007
183
struct tag_revision {
184
    u32 rev;
185
};
186
 
187
#define ATAG_VIDEOLFB   0x54410008
188
struct tag_videolfb {
189
    u16 lfb_width;
190
    u16 lfb_height;
191
    u16 lfb_depth;
192
    u16 lfb_linelength;
193
    u32 lfb_base;
194
    u32 lfb_size;
195
    u8  red_size;
196
    u8  red_pos;
197
    u8  green_size;
198
    u8  green_pos;
199
    u8  blue_size;
200
    u8  blue_pos;
201
    u8  rsvd_size;
202
    u8  rsvd_pos;
203
};
204
 
205
#define ATAG_CMDLINE    0x54410009
206
struct tag_cmdline {
207
    char cmdline[1];
208
};
209
 
210
#define ATAG_ACORN      0x41000101
211
struct tag_acorn {
212
    u32 memc_control_reg;
213
    u32 vram_pages;
214
    u8 sounddefault;
215
    u8 adfsdrives;
216
};
217
 
218
#define ATAG_MEMCLK     0x41000402
219
struct tag_memclk {
220
    u32 fmemclk;
221
};
222
 
223
struct tag {
224
    struct tag_header hdr;
225
    union {
226
        struct tag_core         core;
227
        struct tag_mem32        mem;
228
        struct tag_videotext    videotext;
229
        struct tag_ramdisk      ramdisk;
230
        struct tag_initrd       initrd;
231
        struct tag_serialnr     serialnr;
232
        struct tag_revision     revision;
233
        struct tag_videolfb     videolfb;
234
        struct tag_cmdline      cmdline;
235
 
236
        /*
237
         * Acorn specific
238
         */
239
        struct tag_acorn        acorn;
240
 
241
        /*
242
         * DC21285 specific
243
         */
244
        struct tag_memclk       memclk;
245
    } u;
246
};
247
 
248
// End of inclusion from <asm-arm/setup.h>
249
//=========================================================================
250
 
251
// Default memory layout - can be overridden by platform, typically in
252
// <cyg/hal/plf_io.h>
253
#ifndef CYGHWR_REDBOOT_LINUX_ATAG_MEM
254
#define CYGHWR_REDBOOT_LINUX_ATAG_MEM(_p_)                                                      \
255
    CYG_MACRO_START                                                                             \
256
    /* Next ATAG_MEM. */                                                                        \
257
    _p_->hdr.size = (sizeof(struct tag_mem32) + sizeof(struct tag_header))/sizeof(long);        \
258
    _p_->hdr.tag = ATAG_MEM;                                                                    \
259
    /* Round up so there's only one bit set in the memory size.                                 \
260
     * Don't double it if it's already a power of two, though.                                  \
261
     */                                                                                         \
262
    _p_->u.mem.size  = 1<<hal_msbindex(CYGMEM_REGION_ram_SIZE);                                 \
263
    if (_p_->u.mem.size < CYGMEM_REGION_ram_SIZE)                                               \
264
            _p_->u.mem.size <<= 1;                                                              \
265
    _p_->u.mem.start = CYGARC_PHYSICAL_ADDRESS(CYGMEM_REGION_ram);                              \
266
    CYG_MACRO_END
267
#endif
268
 
269
 
270
// Round up a quantity to a longword (32 bit) length
271
#define ROUNDUP(n) (((n)+3)&~3)
272
 
273
static void
274
do_exec(int argc, char *argv[])
275
{
276
    unsigned long entry;
277
    unsigned long oldints;
278
    bool wait_time_set;
279
    int  wait_time, res;
280
    bool base_addr_set, length_set, cmd_line_set;
281
    bool ramdisk_addr_set, ramdisk_size_set;
282
    unsigned long base_addr, length;
283
    unsigned long ramdisk_addr, ramdisk_size;
284
    struct option_info opts[6];
285
    char line[8];
286
    char *cmd_line;
287
    struct tag *params = (struct tag *)CYGHWR_REDBOOT_ARM_LINUX_TAGS_ADDRESS;
288
    extern char __tramp_start__[], __tramp_end__[];
289
 
290
    // Default physical entry point for Linux is kernel base.
291
    entry = (unsigned long)CYGHWR_REDBOOT_ARM_LINUX_EXEC_ADDRESS;
292
 
293
    base_addr = load_address;
294
    length = load_address_end - load_address;
295
    // Round length up to the next quad word
296
    length = (length + 3) & ~0x3;
297
 
298
    ramdisk_size = 4096*1024;
299
    init_opts(&opts[0], 'w', true, OPTION_ARG_TYPE_NUM,
300
              (void **)&wait_time, (bool *)&wait_time_set, "wait timeout");
301
    init_opts(&opts[1], 'b', true, OPTION_ARG_TYPE_NUM,
302
              (void **)&base_addr, (bool *)&base_addr_set, "base address");
303
    init_opts(&opts[2], 'l', true, OPTION_ARG_TYPE_NUM,
304
              (void **)&length, (bool *)&length_set, "length");
305
    init_opts(&opts[3], 'c', true, OPTION_ARG_TYPE_STR,
306
              (void **)&cmd_line, (bool *)&cmd_line_set, "kernel command line");
307
    init_opts(&opts[4], 'r', true, OPTION_ARG_TYPE_NUM,
308
              (void **)&ramdisk_addr, (bool *)&ramdisk_addr_set, "ramdisk_addr");
309
    init_opts(&opts[5], 's', true, OPTION_ARG_TYPE_NUM,
310
              (void **)&ramdisk_size, (bool *)&ramdisk_size_set, "ramdisk_size");
311
    if (!scan_opts(argc, argv, 1, opts, 6, (void *)&entry, OPTION_ARG_TYPE_NUM, "[physical] starting address"))
312
    {
313
        return;
314
    }
315
 
316
    // Set up parameters to pass to kernel
317
 
318
    // CORE tag must be present & first
319
    params->hdr.size = (sizeof(struct tag_core) + sizeof(struct tag_header))/sizeof(long);
320
    params->hdr.tag = ATAG_CORE;
321
    params->u.core.flags = 0;
322
    params->u.core.pagesize = 0;
323
    params->u.core.rootdev = 0;
324
    params = (struct tag *)((long *)params + params->hdr.size);
325
 
326
    // Fill in the details of the memory layout
327
    CYGHWR_REDBOOT_LINUX_ATAG_MEM(params);
328
 
329
    params = (struct tag *)((long *)params + params->hdr.size);
330
    if (ramdisk_addr_set) {
331
        params->hdr.size = (sizeof(struct tag_initrd) + sizeof(struct tag_header))/sizeof(long);
332
        params->hdr.tag = ATAG_INITRD2;
333
        params->u.initrd.start = CYGARC_PHYSICAL_ADDRESS(ramdisk_addr);
334
        params->u.initrd.size = ramdisk_size;
335
        params = (struct tag *)((long *)params + params->hdr.size);
336
    }
337
    if (cmd_line_set) {
338
        params->hdr.size = (ROUNDUP(strlen(cmd_line)) + sizeof(struct tag_header))/sizeof(long);
339
        params->hdr.tag = ATAG_CMDLINE;
340
        strcpy(params->u.cmdline.cmdline, cmd_line);
341
        params = (struct tag *)((long *)params + params->hdr.size);
342
    }
343
    // Mark end of parameter list
344
    params->hdr.size = 0;
345
    params->hdr.tag = ATAG_NONE;
346
 
347
    if (wait_time_set) {
348
        int script_timeout_ms = wait_time * 1000;
349
#ifdef CYGFUN_REDBOOT_BOOT_SCRIPT
350
        unsigned char *hold_script = script;
351
        script = (unsigned char *)0;
352
#endif
353
        diag_printf("About to start execution at %p - abort with ^C within %d seconds\n",
354
                    (void *)entry, wait_time);
355
        while (script_timeout_ms >= CYGNUM_REDBOOT_CLI_IDLE_TIMEOUT) {
356
            res = _rb_gets(line, sizeof(line), CYGNUM_REDBOOT_CLI_IDLE_TIMEOUT);
357
            if (res == _GETS_CTRLC) {
358
#ifdef CYGFUN_REDBOOT_BOOT_SCRIPT
359
                script = hold_script;  // Re-enable script
360
#endif
361
                return;
362
            }
363
            script_timeout_ms -= CYGNUM_REDBOOT_CLI_IDLE_TIMEOUT;
364
        }
365
    }
366
    if (!base_addr_set) {
367
        if ((base_addr == 0) || (length == 0)) {
368
            // Probably not valid - don't try it
369
            diag_printf("Base address unknown - use \"-b\" option\n");
370
            return;
371
        }
372
        diag_printf("Using base address %p and length %p\n",
373
                    (void*)base_addr, (void*)length);
374
    } else if (base_addr_set && !length_set) {
375
        diag_printf("Length required for non-standard base address\n");
376
        return;
377
    }
378
    HAL_DISABLE_INTERRUPTS(oldints);
379
    HAL_DCACHE_SYNC();
380
    HAL_ICACHE_DISABLE();
381
    HAL_DCACHE_DISABLE();
382
    HAL_DCACHE_SYNC();
383
    HAL_ICACHE_INVALIDATE_ALL();
384
    HAL_DCACHE_INVALIDATE_ALL();
385
 
386
    // Tricky code. We are currently running with the MMU on and the
387
    // memory map possibly convoluted from 1-1.  The trampoline code
388
    // between labels __tramp_start__ and __tramp_end__ must be copied
389
    // to RAM and then executed at the non-mapped address.
390
    // 
391
    // This magic was created in order to be able to execute standard
392
    // Linux kernels with as little change/perturberance as possible.
393
 
394
    // copy the trampline code
395
    memcpy((char *)CYGHWR_REDBOOT_ARM_TRAMPOLINE_ADDRESS,
396
           __tramp_start__,
397
           __tramp_end__ - __tramp_start__);
398
 
399
    asm volatile (
400
        CYGARC_HAL_MMU_OFF(%5)
401
        "__tramp_start__:\n"
402
        " cmp %1,%4;\n"       // Default kernel load address. Relocate
403
        " beq 2f;\n"          // kernel image there if necessary, and
404
        " cmp %2,#0;\n"       // if size is non-zero
405
        " beq 2f;\n"
406
        "1:\n"
407
        " ldr r0,[%1],#4;\n"
408
        " str r0,[%4],#4;\n"
409
        " subs %2,%2,#4;\n"
410
        " bne 1b;\n"
411
        "2:\n"
412
        " mov r0,#0;\n"       // Set board type
413
        " mov r1,%3;\n"       // Machine type
414
        " mov r2,%6;\n"       // Kernel parameters
415
        " mov pc,%0;\n"       // Jump to kernel
416
        "__tramp_end__:\n"
417
        : :
418
        "r"(entry),
419
        "r"(CYGARC_PHYSICAL_ADDRESS(base_addr)),
420
        "r"(length),
421
        "r"(CYGHWR_REDBOOT_ARM_MACHINE_TYPE),
422
        "r"(CYGHWR_REDBOOT_ARM_LINUX_EXEC_ADDRESS),
423
        "r"(CYGARC_PHYSICAL_ADDRESS(CYGHWR_REDBOOT_ARM_TRAMPOLINE_ADDRESS)),
424
        "r"(CYGARC_PHYSICAL_ADDRESS(CYGHWR_REDBOOT_ARM_LINUX_TAGS_ADDRESS))
425
        : "r0", "r1"
426
        );
427
}
428
 
429
#endif // HAL_PLATFORM_MACHINE_TYPE - otherwise we do not support this stuff...
430
 
431
// EOF redboot_linux_exec.c

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.