Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  *  Early boot support code for BootX bootloader
0004  *
0005  *  Copyright (C) 2005 Ben. Herrenschmidt (benh@kernel.crashing.org)
0006  */
0007 
0008 #include <linux/kernel.h>
0009 #include <linux/string.h>
0010 #include <linux/init.h>
0011 #include <linux/of_fdt.h>
0012 #include <generated/utsrelease.h>
0013 #include <asm/sections.h>
0014 #include <asm/prom.h>
0015 #include <asm/page.h>
0016 #include <asm/bootx.h>
0017 #include <asm/btext.h>
0018 #include <asm/io.h>
0019 #include <asm/setup.h>
0020 
0021 #undef DEBUG
0022 #define SET_BOOT_BAT
0023 
0024 #ifdef DEBUG
0025 #define DBG(fmt...) do { bootx_printf(fmt); } while(0)
0026 #else
0027 #define DBG(fmt...) do { } while(0)
0028 #endif
0029 
0030 extern void __start(unsigned long r3, unsigned long r4, unsigned long r5);
0031 
0032 static unsigned long __initdata bootx_dt_strbase;
0033 static unsigned long __initdata bootx_dt_strend;
0034 static unsigned long __initdata bootx_node_chosen;
0035 static boot_infos_t * __initdata bootx_info;
0036 static char __initdata bootx_disp_path[256];
0037 
0038 /* Is boot-info compatible ? */
0039 #define BOOT_INFO_IS_COMPATIBLE(bi) \
0040     ((bi)->compatible_version <= BOOT_INFO_VERSION)
0041 #define BOOT_INFO_IS_V2_COMPATIBLE(bi)  ((bi)->version >= 2)
0042 #define BOOT_INFO_IS_V4_COMPATIBLE(bi)  ((bi)->version >= 4)
0043 
0044 #ifdef CONFIG_BOOTX_TEXT
0045 static void __init bootx_printf(const char *format, ...)
0046 {
0047     const char *p, *q, *s;
0048     va_list args;
0049     unsigned long v;
0050 
0051     va_start(args, format);
0052     for (p = format; *p != 0; p = q) {
0053         for (q = p; *q != 0 && *q != '\n' && *q != '%'; ++q)
0054             ;
0055         if (q > p)
0056             btext_drawtext(p, q - p);
0057         if (*q == 0)
0058             break;
0059         if (*q == '\n') {
0060             ++q;
0061             btext_flushline();
0062             btext_drawstring("\r\n");
0063             btext_flushline();
0064             continue;
0065         }
0066         ++q;
0067         if (*q == 0)
0068             break;
0069         switch (*q) {
0070         case 's':
0071             ++q;
0072             s = va_arg(args, const char *);
0073             if (s == NULL)
0074                 s = "<NULL>";
0075             btext_drawstring(s);
0076             break;
0077         case 'x':
0078             ++q;
0079             v = va_arg(args, unsigned long);
0080             btext_drawhex(v);
0081             break;
0082         }
0083     }
0084     va_end(args);
0085 }
0086 #else /* CONFIG_BOOTX_TEXT */
0087 static void __init bootx_printf(const char *format, ...) {}
0088 #endif /* CONFIG_BOOTX_TEXT */
0089 
0090 static void * __init bootx_early_getprop(unsigned long base,
0091                      unsigned long node,
0092                      char *prop)
0093 {
0094     struct bootx_dt_node *np = (struct bootx_dt_node *)(base + node);
0095     u32 *ppp = &np->properties;
0096 
0097     while(*ppp) {
0098         struct bootx_dt_prop *pp =
0099             (struct bootx_dt_prop *)(base + *ppp);
0100 
0101         if (strcmp((char *)((unsigned long)pp->name + base),
0102                prop) == 0) {
0103             return (void *)((unsigned long)pp->value + base);
0104         }
0105         ppp = &pp->next;
0106     }
0107     return NULL;
0108 }
0109 
0110 #define dt_push_token(token, mem) \
0111     do { \
0112         *(mem) = ALIGN(*(mem),4); \
0113         *((u32 *)*(mem)) = token; \
0114         *(mem) += 4; \
0115     } while(0)
0116 
0117 static unsigned long __init bootx_dt_find_string(char *str)
0118 {
0119     char *s, *os;
0120 
0121     s = os = (char *)bootx_dt_strbase;
0122     s += 4;
0123     while (s <  (char *)bootx_dt_strend) {
0124         if (strcmp(s, str) == 0)
0125             return s - os;
0126         s += strlen(s) + 1;
0127     }
0128     return 0;
0129 }
0130 
0131 static void __init bootx_dt_add_prop(char *name, void *data, int size,
0132                   unsigned long *mem_end)
0133 {
0134     unsigned long soff = bootx_dt_find_string(name);
0135     if (data == NULL)
0136         size = 0;
0137     if (soff == 0) {
0138         bootx_printf("WARNING: Can't find string index for <%s>\n",
0139                  name);
0140         return;
0141     }
0142     if (size > 0x20000) {
0143         bootx_printf("WARNING: ignoring large property ");
0144         bootx_printf("%s length 0x%x\n", name, size);
0145         return;
0146     }
0147     dt_push_token(OF_DT_PROP, mem_end);
0148     dt_push_token(size, mem_end);
0149     dt_push_token(soff, mem_end);
0150 
0151     /* push property content */
0152     if (size && data) {
0153         memcpy((void *)*mem_end, data, size);
0154         *mem_end = ALIGN(*mem_end + size, 4);
0155     }
0156 }
0157 
0158 static void __init bootx_add_chosen_props(unsigned long base,
0159                       unsigned long *mem_end)
0160 {
0161     u32 val;
0162 
0163     bootx_dt_add_prop("linux,bootx", NULL, 0, mem_end);
0164 
0165     if (bootx_info->kernelParamsOffset) {
0166         char *args = (char *)((unsigned long)bootx_info) +
0167             bootx_info->kernelParamsOffset;
0168         bootx_dt_add_prop("bootargs", args, strlen(args) + 1, mem_end);
0169     }
0170     if (bootx_info->ramDisk) {
0171         val = ((unsigned long)bootx_info) + bootx_info->ramDisk;
0172         bootx_dt_add_prop("linux,initrd-start", &val, 4, mem_end);
0173         val += bootx_info->ramDiskSize;
0174         bootx_dt_add_prop("linux,initrd-end", &val, 4, mem_end);
0175     }
0176     if (strlen(bootx_disp_path))
0177         bootx_dt_add_prop("linux,stdout-path", bootx_disp_path,
0178                   strlen(bootx_disp_path) + 1, mem_end);
0179 }
0180 
0181 static void __init bootx_add_display_props(unsigned long base,
0182                        unsigned long *mem_end,
0183                        int has_real_node)
0184 {
0185     boot_infos_t *bi = bootx_info;
0186     u32 tmp;
0187 
0188     if (has_real_node) {
0189         bootx_dt_add_prop("linux,boot-display", NULL, 0, mem_end);
0190         bootx_dt_add_prop("linux,opened", NULL, 0, mem_end);
0191     } else
0192         bootx_dt_add_prop("linux,bootx-noscreen", NULL, 0, mem_end);
0193 
0194     tmp = bi->dispDeviceDepth;
0195     bootx_dt_add_prop("linux,bootx-depth", &tmp, 4, mem_end);
0196     tmp = bi->dispDeviceRect[2] - bi->dispDeviceRect[0];
0197     bootx_dt_add_prop("linux,bootx-width", &tmp, 4, mem_end);
0198     tmp = bi->dispDeviceRect[3] - bi->dispDeviceRect[1];
0199     bootx_dt_add_prop("linux,bootx-height", &tmp, 4, mem_end);
0200     tmp = bi->dispDeviceRowBytes;
0201     bootx_dt_add_prop("linux,bootx-linebytes", &tmp, 4, mem_end);
0202     tmp = (u32)bi->dispDeviceBase;
0203     if (tmp == 0)
0204         tmp = (u32)bi->logicalDisplayBase;
0205     tmp += bi->dispDeviceRect[1] * bi->dispDeviceRowBytes;
0206     tmp += bi->dispDeviceRect[0] * ((bi->dispDeviceDepth + 7) / 8);
0207     bootx_dt_add_prop("linux,bootx-addr", &tmp, 4, mem_end);
0208 }
0209 
0210 static void __init bootx_dt_add_string(char *s, unsigned long *mem_end)
0211 {
0212     unsigned int l = strlen(s) + 1;
0213     memcpy((void *)*mem_end, s, l);
0214     bootx_dt_strend = *mem_end = *mem_end + l;
0215 }
0216 
0217 static void __init bootx_scan_dt_build_strings(unsigned long base,
0218                            unsigned long node,
0219                            unsigned long *mem_end)
0220 {
0221     struct bootx_dt_node *np = (struct bootx_dt_node *)(base + node);
0222     u32 *cpp, *ppp = &np->properties;
0223     unsigned long soff;
0224     char *namep;
0225 
0226     /* Keep refs to known nodes */
0227     namep = np->full_name ? (char *)(base + np->full_name) : NULL;
0228         if (namep == NULL) {
0229         bootx_printf("Node without a full name !\n");
0230         namep = "";
0231     }
0232     DBG("* strings: %s\n", namep);
0233 
0234     if (!strcmp(namep, "/chosen")) {
0235         DBG(" detected /chosen ! adding properties names !\n");
0236         bootx_dt_add_string("linux,bootx", mem_end);
0237         bootx_dt_add_string("linux,stdout-path", mem_end);
0238         bootx_dt_add_string("linux,initrd-start", mem_end);
0239         bootx_dt_add_string("linux,initrd-end", mem_end);
0240         bootx_dt_add_string("bootargs", mem_end);
0241         bootx_node_chosen = node;
0242     }
0243     if (node == bootx_info->dispDeviceRegEntryOffset) {
0244         DBG(" detected display ! adding properties names !\n");
0245         bootx_dt_add_string("linux,boot-display", mem_end);
0246         bootx_dt_add_string("linux,opened", mem_end);
0247         strscpy(bootx_disp_path, namep, sizeof(bootx_disp_path));
0248     }
0249 
0250     /* get and store all property names */
0251     while (*ppp) {
0252         struct bootx_dt_prop *pp =
0253             (struct bootx_dt_prop *)(base + *ppp);
0254 
0255         namep = pp->name ? (char *)(base + pp->name) : NULL;
0256         if (namep == NULL || strcmp(namep, "name") == 0)
0257             goto next;
0258         /* get/create string entry */
0259         soff = bootx_dt_find_string(namep);
0260         if (soff == 0)
0261             bootx_dt_add_string(namep, mem_end);
0262     next:
0263         ppp = &pp->next;
0264     }
0265 
0266     /* do all our children */
0267     cpp = &np->child;
0268     while(*cpp) {
0269         np = (struct bootx_dt_node *)(base + *cpp);
0270         bootx_scan_dt_build_strings(base, *cpp, mem_end);
0271         cpp = &np->sibling;
0272     }
0273 }
0274 
0275 static void __init bootx_scan_dt_build_struct(unsigned long base,
0276                           unsigned long node,
0277                           unsigned long *mem_end)
0278 {
0279     struct bootx_dt_node *np = (struct bootx_dt_node *)(base + node);
0280     u32 *cpp, *ppp = &np->properties;
0281     char *namep, *p, *ep, *lp;
0282     int l;
0283 
0284     dt_push_token(OF_DT_BEGIN_NODE, mem_end);
0285 
0286     /* get the node's full name */
0287     namep = np->full_name ? (char *)(base + np->full_name) : NULL;
0288     if (namep == NULL)
0289         namep = "";
0290     l = strlen(namep);
0291 
0292     DBG("* struct: %s\n", namep);
0293 
0294     /* Fixup an Apple bug where they have bogus \0 chars in the
0295      * middle of the path in some properties, and extract
0296      * the unit name (everything after the last '/').
0297      */
0298     memcpy((void *)*mem_end, namep, l + 1);
0299     namep = (char *)*mem_end;
0300     for (lp = p = namep, ep = namep + l; p < ep; p++) {
0301         if (*p == '/')
0302             lp = namep;
0303         else if (*p != 0)
0304             *lp++ = *p;
0305     }
0306     *lp = 0;
0307     *mem_end = ALIGN((unsigned long)lp + 1, 4);
0308 
0309     /* get and store all properties */
0310     while (*ppp) {
0311         struct bootx_dt_prop *pp =
0312             (struct bootx_dt_prop *)(base + *ppp);
0313 
0314         namep = pp->name ? (char *)(base + pp->name) : NULL;
0315         /* Skip "name" */
0316         if (namep == NULL || !strcmp(namep, "name"))
0317             goto next;
0318         /* Skip "bootargs" in /chosen too as we replace it */
0319         if (node == bootx_node_chosen && !strcmp(namep, "bootargs"))
0320             goto next;
0321 
0322         /* push property head */
0323         bootx_dt_add_prop(namep,
0324                   pp->value ? (void *)(base + pp->value): NULL,
0325                   pp->length, mem_end);
0326     next:
0327         ppp = &pp->next;
0328     }
0329 
0330     if (node == bootx_node_chosen) {
0331         bootx_add_chosen_props(base, mem_end);
0332         if (bootx_info->dispDeviceRegEntryOffset == 0)
0333             bootx_add_display_props(base, mem_end, 0);
0334     }
0335     else if (node == bootx_info->dispDeviceRegEntryOffset)
0336         bootx_add_display_props(base, mem_end, 1);
0337 
0338     /* do all our children */
0339     cpp = &np->child;
0340     while(*cpp) {
0341         np = (struct bootx_dt_node *)(base + *cpp);
0342         bootx_scan_dt_build_struct(base, *cpp, mem_end);
0343         cpp = &np->sibling;
0344     }
0345 
0346     dt_push_token(OF_DT_END_NODE, mem_end);
0347 }
0348 
0349 static unsigned long __init bootx_flatten_dt(unsigned long start)
0350 {
0351     boot_infos_t *bi = bootx_info;
0352     unsigned long mem_start, mem_end;
0353     struct boot_param_header *hdr;
0354     unsigned long base;
0355     u64 *rsvmap;
0356 
0357     /* Start using memory after the big blob passed by BootX, get
0358      * some space for the header
0359      */
0360     mem_start = mem_end = ALIGN(((unsigned long)bi) + start, 4);
0361     DBG("Boot params header at: %x\n", mem_start);
0362     hdr = (struct boot_param_header *)mem_start;
0363     mem_end += sizeof(struct boot_param_header);
0364     rsvmap = (u64 *)(ALIGN(mem_end, 8));
0365     hdr->off_mem_rsvmap = ((unsigned long)rsvmap) - mem_start;
0366     mem_end = ((unsigned long)rsvmap) + 8 * sizeof(u64);
0367 
0368     /* Get base of tree */
0369     base = ((unsigned long)bi) + bi->deviceTreeOffset;
0370 
0371     /* Build string array */
0372     DBG("Building string array at: %x\n", mem_end);
0373     DBG("Device Tree Base=%x\n", base);
0374     bootx_dt_strbase = mem_end;
0375     mem_end += 4;
0376     bootx_dt_strend = mem_end;
0377     bootx_scan_dt_build_strings(base, 4, &mem_end);
0378     /* Add some strings */
0379     bootx_dt_add_string("linux,bootx-noscreen", &mem_end);
0380     bootx_dt_add_string("linux,bootx-depth", &mem_end);
0381     bootx_dt_add_string("linux,bootx-width", &mem_end);
0382     bootx_dt_add_string("linux,bootx-height", &mem_end);
0383     bootx_dt_add_string("linux,bootx-linebytes", &mem_end);
0384     bootx_dt_add_string("linux,bootx-addr", &mem_end);
0385     /* Wrap up strings */
0386     hdr->off_dt_strings = bootx_dt_strbase - mem_start;
0387     hdr->dt_strings_size = bootx_dt_strend - bootx_dt_strbase;
0388 
0389     /* Build structure */
0390     mem_end = ALIGN(mem_end, 16);
0391     DBG("Building device tree structure at: %x\n", mem_end);
0392     hdr->off_dt_struct = mem_end - mem_start;
0393     bootx_scan_dt_build_struct(base, 4, &mem_end);
0394     dt_push_token(OF_DT_END, &mem_end);
0395 
0396     /* Finish header */
0397     hdr->boot_cpuid_phys = 0;
0398     hdr->magic = OF_DT_HEADER;
0399     hdr->totalsize = mem_end - mem_start;
0400     hdr->version = OF_DT_VERSION;
0401     /* Version 16 is not backward compatible */
0402     hdr->last_comp_version = 0x10;
0403 
0404     /* Reserve the whole thing and copy the reserve map in, we
0405      * also bump mem_reserve_cnt to cause further reservations to
0406      * fail since it's too late.
0407      */
0408     mem_end = ALIGN(mem_end, PAGE_SIZE);
0409     DBG("End of boot params: %x\n", mem_end);
0410     rsvmap[0] = mem_start;
0411     rsvmap[1] = mem_end;
0412     if (bootx_info->ramDisk) {
0413         rsvmap[2] = ((unsigned long)bootx_info) + bootx_info->ramDisk;
0414         rsvmap[3] = rsvmap[2] + bootx_info->ramDiskSize;
0415         rsvmap[4] = 0;
0416         rsvmap[5] = 0;
0417     } else {
0418         rsvmap[2] = 0;
0419         rsvmap[3] = 0;
0420     }
0421 
0422     return (unsigned long)hdr;
0423 }
0424 
0425 
0426 #ifdef CONFIG_BOOTX_TEXT
0427 static void __init btext_welcome(boot_infos_t *bi)
0428 {
0429     unsigned long flags;
0430     unsigned long pvr;
0431 
0432     bootx_printf("Welcome to Linux, kernel " UTS_RELEASE "\n");
0433     bootx_printf("\nlinked at        : 0x%x", KERNELBASE);
0434     bootx_printf("\nframe buffer at  : 0x%x", bi->dispDeviceBase);
0435     bootx_printf(" (phys), 0x%x", bi->logicalDisplayBase);
0436     bootx_printf(" (log)");
0437     bootx_printf("\nklimit           : 0x%x",(unsigned long)_end);
0438     bootx_printf("\nboot_info at     : 0x%x", bi);
0439     __asm__ __volatile__ ("mfmsr %0" : "=r" (flags));
0440     bootx_printf("\nMSR              : 0x%x", flags);
0441     __asm__ __volatile__ ("mfspr %0, 287" : "=r" (pvr));
0442     bootx_printf("\nPVR              : 0x%x", pvr);
0443     pvr >>= 16;
0444     if (pvr > 1) {
0445         __asm__ __volatile__ ("mfspr %0, 1008" : "=r" (flags));
0446         bootx_printf("\nHID0             : 0x%x", flags);
0447     }
0448     if (pvr == 8 || pvr == 12 || pvr == 0x800c) {
0449         __asm__ __volatile__ ("mfspr %0, 1019" : "=r" (flags));
0450         bootx_printf("\nICTC             : 0x%x", flags);
0451     }
0452 #ifdef DEBUG
0453     bootx_printf("\n\n");
0454     bootx_printf("bi->deviceTreeOffset   : 0x%x\n",
0455              bi->deviceTreeOffset);
0456     bootx_printf("bi->deviceTreeSize     : 0x%x\n",
0457              bi->deviceTreeSize);
0458 #endif
0459     bootx_printf("\n\n");
0460 }
0461 #endif /* CONFIG_BOOTX_TEXT */
0462 
0463 void __init bootx_init(unsigned long r3, unsigned long r4)
0464 {
0465     boot_infos_t *bi = (boot_infos_t *) r4;
0466     unsigned long hdr;
0467     unsigned long space;
0468     unsigned long ptr;
0469     char *model;
0470     unsigned long offset = reloc_offset();
0471 
0472     reloc_got2(offset);
0473 
0474     bootx_info = bi;
0475 
0476     /* We haven't cleared any bss at this point, make sure
0477      * what we need is initialized
0478      */
0479     bootx_dt_strbase = bootx_dt_strend = 0;
0480     bootx_node_chosen = 0;
0481     bootx_disp_path[0] = 0;
0482 
0483     if (!BOOT_INFO_IS_V2_COMPATIBLE(bi))
0484         bi->logicalDisplayBase = bi->dispDeviceBase;
0485 
0486     /* Fixup depth 16 -> 15 as that's what MacOS calls 16bpp */
0487     if (bi->dispDeviceDepth == 16)
0488         bi->dispDeviceDepth = 15;
0489 
0490 
0491 #ifdef CONFIG_BOOTX_TEXT
0492     ptr = (unsigned long)bi->logicalDisplayBase;
0493     ptr += bi->dispDeviceRect[1] * bi->dispDeviceRowBytes;
0494     ptr += bi->dispDeviceRect[0] * ((bi->dispDeviceDepth + 7) / 8);
0495     btext_setup_display(bi->dispDeviceRect[2] - bi->dispDeviceRect[0],
0496                 bi->dispDeviceRect[3] - bi->dispDeviceRect[1],
0497                 bi->dispDeviceDepth, bi->dispDeviceRowBytes,
0498                 (unsigned long)bi->logicalDisplayBase);
0499     btext_clearscreen();
0500     btext_flushscreen();
0501 #endif /* CONFIG_BOOTX_TEXT */
0502 
0503     /*
0504      * Test if boot-info is compatible.  Done only in config
0505      * CONFIG_BOOTX_TEXT since there is nothing much we can do
0506      * with an incompatible version, except display a message
0507      * and eventually hang the processor...
0508      *
0509      * I'll try to keep enough of boot-info compatible in the
0510      * future to always allow display of this message;
0511      */
0512     if (!BOOT_INFO_IS_COMPATIBLE(bi)) {
0513         bootx_printf(" !!! WARNING - Incompatible version"
0514                  " of BootX !!!\n\n\n");
0515         for (;;)
0516             ;
0517     }
0518     if (bi->architecture != BOOT_ARCH_PCI) {
0519         bootx_printf(" !!! WARNING - Unsupported machine"
0520                  " architecture !\n");
0521         for (;;)
0522             ;
0523     }
0524 
0525 #ifdef CONFIG_BOOTX_TEXT
0526     btext_welcome(bi);
0527 #endif
0528 
0529     /* New BootX enters kernel with MMU off, i/os are not allowed
0530      * here. This hack will have been done by the boostrap anyway.
0531      */
0532     if (bi->version < 4) {
0533         /*
0534          * XXX If this is an iMac, turn off the USB controller.
0535          */
0536         model = (char *) bootx_early_getprop(r4 + bi->deviceTreeOffset,
0537                              4, "model");
0538         if (model
0539             && (strcmp(model, "iMac,1") == 0
0540             || strcmp(model, "PowerMac1,1") == 0)) {
0541             bootx_printf("iMac,1 detected, shutting down USB\n");
0542             out_le32((unsigned __iomem *)0x80880008, 1);    /* XXX */
0543         }
0544     }
0545 
0546     /* Get a pointer that points above the device tree, args, ramdisk,
0547      * etc... to use for generating the flattened tree
0548      */
0549     if (bi->version < 5) {
0550         space = bi->deviceTreeOffset + bi->deviceTreeSize;
0551         if (bi->ramDisk >= space)
0552             space = bi->ramDisk + bi->ramDiskSize;
0553     } else
0554         space = bi->totalParamsSize;
0555 
0556     bootx_printf("Total space used by parameters & ramdisk: 0x%x\n", space);
0557 
0558     /* New BootX will have flushed all TLBs and enters kernel with
0559      * MMU switched OFF, so this should not be useful anymore.
0560      */
0561     if (bi->version < 4) {
0562         unsigned long x __maybe_unused;
0563 
0564         bootx_printf("Touching pages...\n");
0565 
0566         /*
0567          * Touch each page to make sure the PTEs for them
0568          * are in the hash table - the aim is to try to avoid
0569          * getting DSI exceptions while copying the kernel image.
0570          */
0571         for (ptr = ((unsigned long) &_stext) & PAGE_MASK;
0572              ptr < (unsigned long)bi + space; ptr += PAGE_SIZE)
0573             x = *(volatile unsigned long *)ptr;
0574     }
0575 
0576     /* Ok, now we need to generate a flattened device-tree to pass
0577      * to the kernel
0578      */
0579     bootx_printf("Preparing boot params...\n");
0580 
0581     hdr = bootx_flatten_dt(space);
0582 
0583 #ifdef CONFIG_BOOTX_TEXT
0584 #ifdef SET_BOOT_BAT
0585     bootx_printf("Preparing BAT...\n");
0586     btext_prepare_BAT();
0587 #else
0588     btext_unmap();
0589 #endif
0590 #endif
0591 
0592     reloc_got2(-offset);
0593 
0594     __start(hdr, KERNELBASE + offset, 0);
0595 }