uboot/arch/arm/lib/bootm.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0+
   2/* Copyright (C) 2011
   3 * Corscience GmbH & Co. KG - Simon Schwarz <schwarz@corscience.de>
   4 *  - Added prep subcommand support
   5 *  - Reorganized source - modeled after powerpc version
   6 *
   7 * (C) Copyright 2002
   8 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
   9 * Marius Groeger <mgroeger@sysgo.de>
  10 *
  11 * Copyright (C) 2001  Erik Mouw (J.A.K.Mouw@its.tudelft.nl)
  12 */
  13
  14#include <common.h>
  15#include <bootstage.h>
  16#include <command.h>
  17#include <cpu_func.h>
  18#include <dm.h>
  19#include <log.h>
  20#include <asm/global_data.h>
  21#include <dm/root.h>
  22#include <env.h>
  23#include <image.h>
  24#include <u-boot/zlib.h>
  25#include <asm/byteorder.h>
  26#include <linux/libfdt.h>
  27#include <mapmem.h>
  28#include <fdt_support.h>
  29#include <asm/bootm.h>
  30#include <asm/secure.h>
  31#include <linux/compiler.h>
  32#include <bootm.h>
  33#include <vxworks.h>
  34#include <asm/cache.h>
  35
  36#ifdef CONFIG_ARMV7_NONSEC
  37#include <asm/armv7.h>
  38#endif
  39#include <asm/setup.h>
  40
  41DECLARE_GLOBAL_DATA_PTR;
  42
  43static struct tag *params;
  44
  45__weak void board_quiesce_devices(void)
  46{
  47}
  48
  49/**
  50 * announce_and_cleanup() - Print message and prepare for kernel boot
  51 *
  52 * @fake: non-zero to do everything except actually boot
  53 */
  54static void announce_and_cleanup(int fake)
  55{
  56        bootstage_mark_name(BOOTSTAGE_ID_BOOTM_HANDOFF, "start_kernel");
  57#ifdef CONFIG_BOOTSTAGE_FDT
  58        bootstage_fdt_add_report();
  59#endif
  60#ifdef CONFIG_BOOTSTAGE_REPORT
  61        bootstage_report();
  62#endif
  63
  64#ifdef CONFIG_USB_DEVICE
  65        udc_disconnect();
  66#endif
  67
  68        board_quiesce_devices();
  69
  70        printf("\nStarting kernel ...%s\n\n", fake ?
  71                "(fake run for tracing)" : "");
  72        /*
  73         * Call remove function of all devices with a removal flag set.
  74         * This may be useful for last-stage operations, like cancelling
  75         * of DMA operation or releasing device internal buffers.
  76         */
  77        dm_remove_devices_flags(DM_REMOVE_ACTIVE_ALL | DM_REMOVE_NON_VITAL);
  78
  79        /* Remove all active vital devices next */
  80        dm_remove_devices_flags(DM_REMOVE_ACTIVE_ALL);
  81
  82        cleanup_before_linux();
  83}
  84
  85static void setup_start_tag (struct bd_info *bd)
  86{
  87        params = (struct tag *)bd->bi_boot_params;
  88
  89        params->hdr.tag = ATAG_CORE;
  90        params->hdr.size = tag_size (tag_core);
  91
  92        params->u.core.flags = 0;
  93        params->u.core.pagesize = 0;
  94        params->u.core.rootdev = 0;
  95
  96        params = tag_next (params);
  97}
  98
  99static void setup_memory_tags(struct bd_info *bd)
 100{
 101        int i;
 102
 103        for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
 104                params->hdr.tag = ATAG_MEM;
 105                params->hdr.size = tag_size (tag_mem32);
 106
 107                params->u.mem.start = bd->bi_dram[i].start;
 108                params->u.mem.size = bd->bi_dram[i].size;
 109
 110                params = tag_next (params);
 111        }
 112}
 113
 114static void setup_commandline_tag(struct bd_info *bd, char *commandline)
 115{
 116        char *p;
 117
 118        if (!commandline)
 119                return;
 120
 121        /* eat leading white space */
 122        for (p = commandline; *p == ' '; p++);
 123
 124        /* skip non-existent command lines so the kernel will still
 125         * use its default command line.
 126         */
 127        if (*p == '\0')
 128                return;
 129
 130        params->hdr.tag = ATAG_CMDLINE;
 131        params->hdr.size =
 132                (sizeof (struct tag_header) + strlen (p) + 1 + 4) >> 2;
 133
 134        strcpy (params->u.cmdline.cmdline, p);
 135
 136        params = tag_next (params);
 137}
 138
 139static void setup_initrd_tag(struct bd_info *bd, ulong initrd_start,
 140                             ulong initrd_end)
 141{
 142        /* an ATAG_INITRD node tells the kernel where the compressed
 143         * ramdisk can be found. ATAG_RDIMG is a better name, actually.
 144         */
 145        params->hdr.tag = ATAG_INITRD2;
 146        params->hdr.size = tag_size (tag_initrd);
 147
 148        params->u.initrd.start = initrd_start;
 149        params->u.initrd.size = initrd_end - initrd_start;
 150
 151        params = tag_next (params);
 152}
 153
 154static void setup_serial_tag(struct tag **tmp)
 155{
 156        struct tag *params = *tmp;
 157        struct tag_serialnr serialnr;
 158
 159        get_board_serial(&serialnr);
 160        params->hdr.tag = ATAG_SERIAL;
 161        params->hdr.size = tag_size (tag_serialnr);
 162        params->u.serialnr.low = serialnr.low;
 163        params->u.serialnr.high= serialnr.high;
 164        params = tag_next (params);
 165        *tmp = params;
 166}
 167
 168static void setup_revision_tag(struct tag **in_params)
 169{
 170        u32 rev = 0;
 171
 172        rev = get_board_rev();
 173        params->hdr.tag = ATAG_REVISION;
 174        params->hdr.size = tag_size (tag_revision);
 175        params->u.revision.rev = rev;
 176        params = tag_next (params);
 177}
 178
 179static void setup_end_tag(struct bd_info *bd)
 180{
 181        params->hdr.tag = ATAG_NONE;
 182        params->hdr.size = 0;
 183}
 184
 185__weak void setup_board_tags(struct tag **in_params) {}
 186
 187#ifdef CONFIG_ARM64
 188static void do_nonsec_virt_switch(void)
 189{
 190        smp_kick_all_cpus();
 191        dcache_disable();       /* flush cache before swtiching to EL2 */
 192}
 193#endif
 194
 195__weak void board_prep_linux(bootm_headers_t *images) { }
 196
 197/* Subcommand: PREP */
 198static void boot_prep_linux(bootm_headers_t *images)
 199{
 200        char *commandline = env_get("bootargs");
 201
 202        if (CONFIG_IS_ENABLED(OF_LIBFDT) && images->ft_len) {
 203#ifdef CONFIG_OF_LIBFDT
 204                debug("using: FDT\n");
 205                if (image_setup_linux(images)) {
 206                        panic("FDT creation failed!");
 207                }
 208#endif
 209        } else if (BOOTM_ENABLE_TAGS) {
 210                debug("using: ATAGS\n");
 211                setup_start_tag(gd->bd);
 212                if (BOOTM_ENABLE_SERIAL_TAG)
 213                        setup_serial_tag(&params);
 214                if (BOOTM_ENABLE_CMDLINE_TAG)
 215                        setup_commandline_tag(gd->bd, commandline);
 216                if (BOOTM_ENABLE_REVISION_TAG)
 217                        setup_revision_tag(&params);
 218                if (BOOTM_ENABLE_MEMORY_TAGS)
 219                        setup_memory_tags(gd->bd);
 220                if (BOOTM_ENABLE_INITRD_TAG) {
 221                        /*
 222                         * In boot_ramdisk_high(), it may relocate ramdisk to
 223                         * a specified location. And set images->initrd_start &
 224                         * images->initrd_end to relocated ramdisk's start/end
 225                         * addresses. So use them instead of images->rd_start &
 226                         * images->rd_end when possible.
 227                         */
 228                        if (images->initrd_start && images->initrd_end) {
 229                                setup_initrd_tag(gd->bd, images->initrd_start,
 230                                                 images->initrd_end);
 231                        } else if (images->rd_start && images->rd_end) {
 232                                setup_initrd_tag(gd->bd, images->rd_start,
 233                                                 images->rd_end);
 234                        }
 235                }
 236                setup_board_tags(&params);
 237                setup_end_tag(gd->bd);
 238        } else {
 239                panic("FDT and ATAGS support not compiled in\n");
 240        }
 241
 242        board_prep_linux(images);
 243}
 244
 245__weak bool armv7_boot_nonsec_default(void)
 246{
 247#ifdef CONFIG_ARMV7_BOOT_SEC_DEFAULT
 248        return false;
 249#else
 250        return true;
 251#endif
 252}
 253
 254#ifdef CONFIG_ARMV7_NONSEC
 255bool armv7_boot_nonsec(void)
 256{
 257        char *s = env_get("bootm_boot_mode");
 258        bool nonsec = armv7_boot_nonsec_default();
 259
 260        if (s && !strcmp(s, "sec"))
 261                nonsec = false;
 262
 263        if (s && !strcmp(s, "nonsec"))
 264                nonsec = true;
 265
 266        return nonsec;
 267}
 268#endif
 269
 270#ifdef CONFIG_ARM64
 271__weak void update_os_arch_secondary_cores(uint8_t os_arch)
 272{
 273}
 274
 275#ifdef CONFIG_ARMV8_SWITCH_TO_EL1
 276static void switch_to_el1(void)
 277{
 278        if ((IH_ARCH_DEFAULT == IH_ARCH_ARM64) &&
 279            (images.os.arch == IH_ARCH_ARM))
 280                armv8_switch_to_el1(0, (u64)gd->bd->bi_arch_number,
 281                                    (u64)images.ft_addr, 0,
 282                                    (u64)images.ep,
 283                                    ES_TO_AARCH32);
 284        else
 285                armv8_switch_to_el1((u64)images.ft_addr, 0, 0, 0,
 286                                    images.ep,
 287                                    ES_TO_AARCH64);
 288}
 289#endif
 290#endif
 291
 292/* Subcommand: GO */
 293static void boot_jump_linux(bootm_headers_t *images, int flag)
 294{
 295#ifdef CONFIG_ARM64
 296        void (*kernel_entry)(void *fdt_addr, void *res0, void *res1,
 297                        void *res2);
 298        int fake = (flag & BOOTM_STATE_OS_FAKE_GO);
 299
 300        kernel_entry = (void (*)(void *fdt_addr, void *res0, void *res1,
 301                                void *res2))images->ep;
 302
 303        debug("## Transferring control to Linux (at address %lx)...\n",
 304                (ulong) kernel_entry);
 305        bootstage_mark(BOOTSTAGE_ID_RUN_OS);
 306
 307        announce_and_cleanup(fake);
 308
 309        if (!fake) {
 310#ifdef CONFIG_ARMV8_PSCI
 311                armv8_setup_psci();
 312#endif
 313                do_nonsec_virt_switch();
 314
 315                update_os_arch_secondary_cores(images->os.arch);
 316
 317#ifdef CONFIG_ARMV8_SWITCH_TO_EL1
 318                armv8_switch_to_el2((u64)images->ft_addr, 0, 0, 0,
 319                                    (u64)switch_to_el1, ES_TO_AARCH64);
 320#else
 321                if ((IH_ARCH_DEFAULT == IH_ARCH_ARM64) &&
 322                    (images->os.arch == IH_ARCH_ARM))
 323                        armv8_switch_to_el2(0, (u64)gd->bd->bi_arch_number,
 324                                            (u64)images->ft_addr, 0,
 325                                            (u64)images->ep,
 326                                            ES_TO_AARCH32);
 327                else
 328                        armv8_switch_to_el2((u64)images->ft_addr, 0, 0, 0,
 329                                            images->ep,
 330                                            ES_TO_AARCH64);
 331#endif
 332        }
 333#else
 334        unsigned long machid = gd->bd->bi_arch_number;
 335        char *s;
 336        void (*kernel_entry)(int zero, int arch, uint params);
 337        unsigned long r2;
 338        int fake = (flag & BOOTM_STATE_OS_FAKE_GO);
 339
 340        kernel_entry = (void (*)(int, int, uint))images->ep;
 341#ifdef CONFIG_CPU_V7M
 342        ulong addr = (ulong)kernel_entry | 1;
 343        kernel_entry = (void *)addr;
 344#endif
 345        s = env_get("machid");
 346        if (s) {
 347                if (strict_strtoul(s, 16, &machid) < 0) {
 348                        debug("strict_strtoul failed!\n");
 349                        return;
 350                }
 351                printf("Using machid 0x%lx from environment\n", machid);
 352        }
 353
 354        debug("## Transferring control to Linux (at address %08lx)" \
 355                "...\n", (ulong) kernel_entry);
 356        bootstage_mark(BOOTSTAGE_ID_RUN_OS);
 357        announce_and_cleanup(fake);
 358
 359        if (CONFIG_IS_ENABLED(OF_LIBFDT) && images->ft_len)
 360                r2 = (unsigned long)images->ft_addr;
 361        else
 362                r2 = gd->bd->bi_boot_params;
 363
 364        if (!fake) {
 365#ifdef CONFIG_ARMV7_NONSEC
 366                if (armv7_boot_nonsec()) {
 367                        armv7_init_nonsec();
 368                        secure_ram_addr(_do_nonsec_entry)(kernel_entry,
 369                                                          0, machid, r2);
 370                } else
 371#endif
 372                        kernel_entry(0, machid, r2);
 373        }
 374#endif
 375}
 376
 377/* Main Entry point for arm bootm implementation
 378 *
 379 * Modeled after the powerpc implementation
 380 * DIFFERENCE: Instead of calling prep and go at the end
 381 * they are called if subcommand is equal 0.
 382 */
 383int do_bootm_linux(int flag, int argc, char *const argv[],
 384                   bootm_headers_t *images)
 385{
 386        /* No need for those on ARM */
 387        if (flag & BOOTM_STATE_OS_BD_T || flag & BOOTM_STATE_OS_CMDLINE)
 388                return -1;
 389
 390        if (flag & BOOTM_STATE_OS_PREP) {
 391                boot_prep_linux(images);
 392                return 0;
 393        }
 394
 395        if (flag & (BOOTM_STATE_OS_GO | BOOTM_STATE_OS_FAKE_GO)) {
 396                boot_jump_linux(images, flag);
 397                return 0;
 398        }
 399
 400        boot_prep_linux(images);
 401        boot_jump_linux(images, flag);
 402        return 0;
 403}
 404
 405#if defined(CONFIG_BOOTM_VXWORKS)
 406void boot_prep_vxworks(bootm_headers_t *images)
 407{
 408#if defined(CONFIG_OF_LIBFDT)
 409        int off;
 410
 411        if (images->ft_addr) {
 412                off = fdt_path_offset(images->ft_addr, "/memory");
 413                if (off > 0) {
 414                        if (arch_fixup_fdt(images->ft_addr))
 415                                puts("## WARNING: fixup memory failed!\n");
 416                }
 417        }
 418#endif
 419        cleanup_before_linux();
 420}
 421void boot_jump_vxworks(bootm_headers_t *images)
 422{
 423#if defined(CONFIG_ARM64) && defined(CONFIG_ARMV8_PSCI)
 424        armv8_setup_psci();
 425        smp_kick_all_cpus();
 426#endif
 427
 428        /* ARM VxWorks requires device tree physical address to be passed */
 429        ((void (*)(void *))images->ep)(images->ft_addr);
 430}
 431#endif
 432