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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [hal/] [arm/] [arch/] [current/] [src/] [redboot_linux_exec.c] - Blame information for rev 786

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 786 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):    gthomas
52
// Contributors: gthomas, jskov,
53
//               Russell King <rmk@arm.linux.org.uk>
54
// Date:         2001-02-20
55
// Purpose:      
56
// Description:  
57
//              
58
// This code is part of RedBoot (tm).
59
//
60
//####DESCRIPTIONEND####
61
//
62
//==========================================================================
63
 
64
#include <pkgconf/hal.h>
65
#include <redboot.h>
66
 
67
#ifdef CYGPKG_IO_ETH_DRIVERS
68
#include <cyg/io/eth/eth_drv.h>            // Logical driver interfaces
69
#endif
70
 
71
#include <cyg/hal/hal_intr.h>
72
#include <cyg/hal/hal_cache.h>
73
#include CYGHWR_MEMORY_LAYOUT_H
74
 
75
#include <cyg/hal/hal_io.h>
76
 
77
#ifndef CYGARC_PHYSICAL_ADDRESS
78
# error
79
# define CYGARC_PHYSICAL_ADDRESS(x) (x)
80
#endif
81
 
82
// FIXME: This should be a CDL variable, and CYGSEM_REDBOOT_ARM_LINUX_BOOT
83
//        active_if  CYGHWR_HAL_ARM_REDBOOT_MACHINE_TYPE>0
84
#ifdef HAL_PLATFORM_MACHINE_TYPE
85
#define CYGHWR_REDBOOT_ARM_MACHINE_TYPE HAL_PLATFORM_MACHINE_TYPE
86
 
87
// Exported CLI function(s)
88
static void do_exec(int argc, char *argv[]);
89
RedBoot_cmd("exec",
90
            "Execute an image - with MMU off",
91
            "[-w timeout] [-b <load addr> [-l <length>]]\n"
92
            "        [-r <ramdisk addr> [-s <ramdisk length>]]\n"
93
            "        [-c \"kernel command line\"] [-t <target> ] [<entry_point>]",
94
            do_exec
95
    );
96
 
97
// CYGARC_HAL_MMU_OFF inserts code to turn off MMU and jump to a physical
98
// address. Some ARM implementations may need special handling and define
99
// their own version.
100
#ifndef CYGARC_HAL_MMU_OFF
101
 
102
#define __CYGARC_GET_CTLREG \
103
              "   mrc p15,0,r0,c1,c0,0\n"
104
 
105
#define __CYGARC_CLR_MMU_BITS         \
106
  "   bic r0,r0,#0xd\n"               \
107
  "   bic r0,r0,#0x1000\n"            \
108
 
109
#ifdef CYGHWR_HAL_ARM_BIGENDIAN
110
#define __CYGARC_CLR_MMU_BITS_X       \
111
  "   bic r0,r0,#0x8d\n"              \
112
  "   bic r0,r0,#0x1000\n"
113
#else
114
#define __CYGARC_CLR_MMU_BITS_X       \
115
  "   bic r0,r0,#0xd\n"               \
116
  "   bic r0,r0,#0x1000\n"            \
117
  "   orr r0,r0,#0x80\n"
118
#endif
119
 
120
#define __CYGARC_SET_CTLREG(__paddr__) \
121
  "   mcr p15,0,r0,c1,c0,0\n"          \
122
  "   mov pc," #__paddr__ "\n"
123
 
124
#define CYGARC_HAL_MMU_OFF(__paddr__)  \
125
  "   mcr p15,0,r0,c7,c10,4\n"         \
126
  "   mcr p15,0,r0,c7,c7,0\n"          \
127
  __CYGARC_GET_CTLREG                  \
128
  __CYGARC_CLR_MMU_BITS                \
129
  __CYGARC_SET_CTLREG(__paddr__)
130
 
131
#define CYGARC_HAL_MMU_OFF_X(__paddr__)  \
132
  "   mcr p15,0,r0,c7,c10,4\n"           \
133
  "   mcr p15,0,r0,c7,c7,0\n"            \
134
  __CYGARC_GET_CTLREG                    \
135
  __CYGARC_CLR_MMU_BITS_X                \
136
  __CYGARC_SET_CTLREG(__paddr__)
137
 
138
#endif  // CYGARC_HAL_MMU_OFF
139
 
140
//
141
// Parameter info for Linux kernel
142
//   ** C A U T I O N **  This setup must match "asm-arm/setup.h"
143
//
144
// Info is passed at a fixed location, using a sequence of tagged
145
// data entries.
146
//
147
 
148
typedef unsigned long  u32;
149
typedef unsigned short u16;
150
typedef unsigned char  u8;
151
 
152
//=========================================================================
153
//  From Linux <asm-arm/setup.h>
154
 
155
#define ATAG_NONE       0x00000000
156
struct tag_header {
157
    u32 size;    // Size of tag (hdr+data) in *longwords*
158
    u32 tag;
159
};
160
 
161
#define ATAG_CORE       0x54410001
162
struct tag_core {
163
    u32 flags;          /* bit 0 = read-only */
164
    u32 pagesize;
165
    u32 rootdev;
166
};
167
 
168
#define ATAG_MEM                0x54410002
169
struct tag_mem32 {
170
    u32 size;
171
    u32 start;
172
};
173
 
174
#define ATAG_VIDEOTEXT  0x54410003
175
struct tag_videotext {
176
    u8   x;
177
    u8   y;
178
    u16  video_page;
179
    u8   video_mode;
180
    u8   video_cols;
181
    u16  video_ega_bx;
182
    u8   video_lines;
183
    u8   video_isvga;
184
    u16  video_points;
185
};
186
 
187
#define ATAG_RAMDISK    0x54410004
188
struct tag_ramdisk {
189
    u32 flags;          /* b0 = load, b1 = prompt */
190
    u32 size;
191
    u32 start;
192
};
193
 
194
/*
195
 * this one accidentally used virtual addresses - as such,
196
 * its deprecated.
197
 */
198
#define ATAG_INITRD     0x54410005
199
 
200
/* describes where the compressed ramdisk image lives (physical address) */
201
#define ATAG_INITRD2    0x54420005
202
struct tag_initrd {
203
    u32 start;
204
    u32 size;
205
};
206
 
207
#define ATAG_SERIAL     0x54410006
208
struct tag_serialnr {
209
    u32 low;
210
    u32 high;
211
};
212
 
213
#define ATAG_REVISION   0x54410007
214
struct tag_revision {
215
    u32 rev;
216
};
217
 
218
#define ATAG_VIDEOLFB   0x54410008
219
struct tag_videolfb {
220
    u16 lfb_width;
221
    u16 lfb_height;
222
    u16 lfb_depth;
223
    u16 lfb_linelength;
224
    u32 lfb_base;
225
    u32 lfb_size;
226
    u8  red_size;
227
    u8  red_pos;
228
    u8  green_size;
229
    u8  green_pos;
230
    u8  blue_size;
231
    u8  blue_pos;
232
    u8  rsvd_size;
233
    u8  rsvd_pos;
234
};
235
 
236
#define ATAG_CMDLINE    0x54410009
237
struct tag_cmdline {
238
    char cmdline[1];
239
};
240
 
241
#define ATAG_ACORN      0x41000101
242
struct tag_acorn {
243
    u32 memc_control_reg;
244
    u32 vram_pages;
245
    u8 sounddefault;
246
    u8 adfsdrives;
247
};
248
 
249
#define ATAG_MEMCLK     0x41000402
250
struct tag_memclk {
251
    u32 fmemclk;
252
};
253
 
254
struct tag {
255
    struct tag_header hdr;
256
    union {
257
        struct tag_core         core;
258
        struct tag_mem32        mem;
259
        struct tag_videotext    videotext;
260
        struct tag_ramdisk      ramdisk;
261
        struct tag_initrd       initrd;
262
        struct tag_serialnr     serialnr;
263
        struct tag_revision     revision;
264
        struct tag_videolfb     videolfb;
265
        struct tag_cmdline      cmdline;
266
 
267
        /*
268
         * Acorn specific
269
         */
270
        struct tag_acorn        acorn;
271
 
272
        /*
273
         * DC21285 specific
274
         */
275
        struct tag_memclk       memclk;
276
    } u;
277
};
278
 
279
// End of inclusion from <asm-arm/setup.h>
280
//=========================================================================
281
 
282
// Default memory layout - can be overridden by platform, typically in
283
// <cyg/hal/plf_io.h>
284
#ifndef CYGHWR_REDBOOT_LINUX_ATAG_MEM
285
#define CYGHWR_REDBOOT_LINUX_ATAG_MEM(_p_)                                                      \
286
    CYG_MACRO_START                                                                             \
287
    /* Next ATAG_MEM. */                                                                        \
288
    _p_->hdr.size = (sizeof(struct tag_mem32) + sizeof(struct tag_header))/sizeof(long);        \
289
    _p_->hdr.tag = ATAG_MEM;                                                                    \
290
    /* Round up so there's only one bit set in the memory size.                                 \
291
     * Don't double it if it's already a power of two, though.                                  \
292
     */                                                                                         \
293
    _p_->u.mem.size  = 1<<hal_msbindex(CYGMEM_REGION_ram_SIZE);                                 \
294
    if (_p_->u.mem.size < CYGMEM_REGION_ram_SIZE)                                               \
295
            _p_->u.mem.size <<= 1;                                                              \
296
    _p_->u.mem.start = CYGARC_PHYSICAL_ADDRESS(CYGMEM_REGION_ram);                              \
297
    CYG_MACRO_END
298
#endif
299
 
300
 
301
// Round up a quantity to a longword (32 bit) length
302
#define ROUNDUP(n) (((n)+3)&~3)
303
 
304
static void
305
do_exec(int argc, char *argv[])
306
{
307
    unsigned long entry;
308
    unsigned long target;
309
    unsigned long oldints;
310
    bool wait_time_set;
311
    int  wait_time, res, num_opts;
312
    bool base_addr_set, length_set, cmd_line_set;
313
    bool ramdisk_addr_set, ramdisk_size_set;
314
    unsigned long base_addr, length;
315
    unsigned long ramdisk_addr, ramdisk_size;
316
    struct option_info opts[7];
317
    char line[8];
318
    char *cmd_line;
319
    struct tag *params = (struct tag *)CYGHWR_REDBOOT_ARM_LINUX_TAGS_ADDRESS;
320
#ifdef CYGHWR_REDBOOT_LINUX_EXEC_X_SWITCH
321
    bool swap_endian;
322
    extern char __xtramp_start__[], __xtramp_end__[];
323
#endif
324
    extern char __tramp_start__[], __tramp_end__[];
325
 
326
    // Check to see if a valid image has been loaded
327
    if (entry_address == (unsigned long)NO_MEMORY) {
328
        diag_printf("Can't execute Linux - invalid entry address\n");
329
        return;
330
    }
331
    // Default physical entry point for Linux is kernel base.
332
    entry = (unsigned long)CYGHWR_REDBOOT_ARM_LINUX_EXEC_ADDRESS;
333
    target = (unsigned long)CYGHWR_REDBOOT_ARM_LINUX_EXEC_ADDRESS;
334
 
335
    base_addr = load_address;
336
    length = load_address_end - load_address;
337
    // Round length up to the next quad word
338
    length = (length + 3) & ~0x3;
339
 
340
    ramdisk_size = 4096*1024;
341
    init_opts(&opts[0], 'w', true, OPTION_ARG_TYPE_NUM,
342
              (void **)&wait_time, (bool *)&wait_time_set, "wait timeout");
343
    init_opts(&opts[1], 'b', true, OPTION_ARG_TYPE_NUM,
344
              (void **)&base_addr, (bool *)&base_addr_set, "base address");
345
    init_opts(&opts[2], 'l', true, OPTION_ARG_TYPE_NUM,
346
              (void **)&length, (bool *)&length_set, "length");
347
    init_opts(&opts[3], 'c', true, OPTION_ARG_TYPE_STR,
348
              (void **)&cmd_line, (bool *)&cmd_line_set, "kernel command line");
349
    init_opts(&opts[4], 'r', true, OPTION_ARG_TYPE_NUM,
350
              (void **)&ramdisk_addr, (bool *)&ramdisk_addr_set, "ramdisk_addr");
351
    init_opts(&opts[5], 's', true, OPTION_ARG_TYPE_NUM,
352
              (void **)&ramdisk_size, (bool *)&ramdisk_size_set, "ramdisk_size");
353
    init_opts(&opts[6], 't', true, OPTION_ARG_TYPE_NUM,
354
              &target, 0, "[physical] target address");
355
    num_opts = 7;
356
#ifdef CYGHWR_REDBOOT_LINUX_EXEC_X_SWITCH
357
    init_opts(&opts[num_opts], 'x', false, OPTION_ARG_TYPE_FLG,
358
              (void **)&swap_endian, 0, "swap endianess");
359
    ++num_opts;
360
#endif
361
    if (!scan_opts(argc, argv, 1, opts, num_opts, (void *)&entry, OPTION_ARG_TYPE_NUM, "[physical] starting address"))
362
    {
363
        return;
364
    }
365
 
366
    // Set up parameters to pass to kernel
367
 
368
    // CORE tag must be present & first
369
    params->hdr.size = (sizeof(struct tag_core) + sizeof(struct tag_header))/sizeof(long);
370
    params->hdr.tag = ATAG_CORE;
371
    params->u.core.flags = 0;
372
    params->u.core.pagesize = 0;
373
    params->u.core.rootdev = 0;
374
    params = (struct tag *)((long *)params + params->hdr.size);
375
 
376
    // Fill in the details of the memory layout
377
    CYGHWR_REDBOOT_LINUX_ATAG_MEM(params);
378
 
379
    params = (struct tag *)((long *)params + params->hdr.size);
380
    if (ramdisk_addr_set) {
381
        params->hdr.size = (sizeof(struct tag_initrd) + sizeof(struct tag_header))/sizeof(long);
382
        params->hdr.tag = ATAG_INITRD2;
383
        params->u.initrd.start = CYGARC_PHYSICAL_ADDRESS(ramdisk_addr);
384
        params->u.initrd.size = ramdisk_size;
385
        params = (struct tag *)((long *)params + params->hdr.size);
386
    }
387
    if (cmd_line_set) {
388
        params->hdr.size = (ROUNDUP(strlen(cmd_line)) + sizeof(struct tag_header))/sizeof(long);
389
        params->hdr.tag = ATAG_CMDLINE;
390
        strcpy(params->u.cmdline.cmdline, cmd_line);
391
        params = (struct tag *)((long *)params + params->hdr.size);
392
    }
393
    // Mark end of parameter list
394
    params->hdr.size = 0;
395
    params->hdr.tag = ATAG_NONE;
396
 
397
    if (wait_time_set) {
398
        int script_timeout_ms = wait_time * 1000;
399
#ifdef CYGFUN_REDBOOT_BOOT_SCRIPT
400
        unsigned char *hold_script = script;
401
        script = (unsigned char *)0;
402
#endif
403
        diag_printf("About to start execution of image at %p, entry point %p - abort with ^C within %d seconds\n",
404
                    (void *)target, (void *)entry, wait_time);
405
        while (script_timeout_ms >= CYGNUM_REDBOOT_CLI_IDLE_TIMEOUT) {
406
            res = _rb_gets(line, sizeof(line), CYGNUM_REDBOOT_CLI_IDLE_TIMEOUT);
407
            if (res == _GETS_CTRLC) {
408
#ifdef CYGFUN_REDBOOT_BOOT_SCRIPT
409
                script = hold_script;  // Re-enable script
410
#endif
411
                return;
412
            }
413
            script_timeout_ms -= CYGNUM_REDBOOT_CLI_IDLE_TIMEOUT;
414
        }
415
    }
416
    if (!base_addr_set) {
417
        if ((base_addr == 0) || (length == 0)) {
418
            // Probably not valid - don't try it
419
            diag_printf("Base address unknown - use \"-b\" option\n");
420
            return;
421
        }
422
        diag_printf("Using base address %p and length %p\n",
423
                    (void*)base_addr, (void*)length);
424
    } else if (base_addr_set && !length_set) {
425
        diag_printf("Length required for non-standard base address\n");
426
        return;
427
    }
428
 
429
#ifdef CYGPKG_IO_ETH_DRIVERS
430
    eth_drv_stop();
431
#endif
432
 
433
    HAL_DISABLE_INTERRUPTS(oldints);
434
    HAL_DCACHE_SYNC();
435
    HAL_ICACHE_DISABLE();
436
    HAL_DCACHE_DISABLE();
437
    HAL_DCACHE_SYNC();
438
    HAL_ICACHE_INVALIDATE_ALL();
439
    HAL_DCACHE_INVALIDATE_ALL();
440
 
441
    // Tricky code. We are currently running with the MMU on and the
442
    // memory map possibly convoluted from 1-1.  The trampoline code
443
    // between labels __tramp_start__ and __tramp_end__ must be copied
444
    // to RAM and then executed at the non-mapped address.
445
    // 
446
    // This magic was created in order to be able to execute standard
447
    // Linux kernels with as little change/perturberance as possible.
448
 
449
#ifdef CYGHWR_REDBOOT_LINUX_EXEC_X_SWITCH
450
    if (swap_endian) {
451
        // copy the trampline code
452
        memcpy((char *)CYGHWR_REDBOOT_ARM_TRAMPOLINE_ADDRESS,
453
               __xtramp_start__,
454
               __xtramp_end__ - __xtramp_start__);
455
 
456
        asm volatile (
457
            CYGARC_HAL_MMU_OFF_X(%5)
458
            "__xtramp_start__:\n"
459
            " cmp %1,%4;\n"       // Default kernel load address. Relocate
460
            " beq 2f;\n"          // kernel image there if necessary, and
461
            " cmp %2,#0;\n"       // if size is non-zero
462
            " beq 2f;\n"
463
            "1:\n"
464
            " ldr r0,[%1],#4;\n"
465
            " eor %5, r0, r0, ror #16;\n"
466
            " bic %5, %5, #0x00ff0000;\n"
467
            " mov r0, r0, ror #8;\n"
468
            " eor r0, r0, %5, lsr #8;\n"
469
            " str r0,[%4],#4;\n"
470
            " subs %2,%2,#4;\n"
471
            " bne 1b;\n"
472
            "2:\n"
473
            " mov r0,#0;\n"       // Set board type
474
            " mov r1,%3;\n"       // Machine type
475
            " mov r2,%6;\n"       // Kernel parameters
476
            " mov pc,%0;\n"       // Jump to kernel
477
            "__xtramp_end__:\n"
478
            : :
479
            "r"(entry),
480
            "r"(CYGARC_PHYSICAL_ADDRESS(base_addr)),
481
            "r"(length),
482
            "r"(CYGHWR_REDBOOT_ARM_MACHINE_TYPE),
483
            "r"(CYGHWR_REDBOOT_ARM_LINUX_EXEC_ADDRESS),
484
            "r"(CYGARC_PHYSICAL_ADDRESS(CYGHWR_REDBOOT_ARM_TRAMPOLINE_ADDRESS)),
485
            "r"(CYGARC_PHYSICAL_ADDRESS(CYGHWR_REDBOOT_ARM_LINUX_TAGS_ADDRESS))
486
            : "r0", "r1"
487
            );
488
    }
489
#endif // CYGHWR_REDBOOT_LINUX_EXEC_X_SWITCH
490
 
491
    // copy the trampline code
492
    memcpy((char *)CYGHWR_REDBOOT_ARM_TRAMPOLINE_ADDRESS,
493
           __tramp_start__,
494
           __tramp_end__ - __tramp_start__);
495
 
496
    asm volatile (
497
        CYGARC_HAL_MMU_OFF(%5)
498
        "__tramp_start__:\n"
499
        " cmp %1,%4;\n"       // Default kernel load address. Relocate
500
        " beq 2f;\n"          // kernel image there if necessary, and
501
        " cmp %2,#0;\n"       // if size is non-zero
502
        " beq 2f;\n"
503
        "1:\n"
504
        " ldr r0,[%1],#4;\n"
505
        " str r0,[%4],#4;\n"
506
        " subs %2,%2,#4;\n"
507
        " bne 1b;\n"
508
        "2:\n"
509
        " mov r0,#0;\n"       // Set board type
510
        " mov r1,%3;\n"       // Machine type
511
        " mov r2,%6;\n"       // Kernel parameters
512
        " mov pc,%0;\n"       // Jump to kernel
513
        "__tramp_end__:\n"
514
        : :
515
        "r"(entry),
516
        "r"(CYGARC_PHYSICAL_ADDRESS(base_addr)),
517
        "r"(length),
518
        "r"(CYGHWR_REDBOOT_ARM_MACHINE_TYPE),
519
        "r"(target),
520
        "r"(CYGARC_PHYSICAL_ADDRESS(CYGHWR_REDBOOT_ARM_TRAMPOLINE_ADDRESS)),
521
        "r"(CYGARC_PHYSICAL_ADDRESS(CYGHWR_REDBOOT_ARM_LINUX_TAGS_ADDRESS))
522
        : "r0", "r1"
523
        );
524
}
525
 
526
#endif // HAL_PLATFORM_MACHINE_TYPE - otherwise we do not support this stuff...
527
 
528
// 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.