Path: blob/master/arch/powerpc/platforms/powermac/bootx_init.c
26481 views
// SPDX-License-Identifier: GPL-2.0-or-later1/*2* Early boot support code for BootX bootloader3*4* Copyright (C) 2005 Ben. Herrenschmidt ([email protected])5*/67#include <linux/kernel.h>8#include <linux/string.h>9#include <linux/init.h>10#include <linux/of_fdt.h>11#include <generated/utsrelease.h>12#include <asm/sections.h>13#include <asm/prom.h>14#include <asm/page.h>15#include <asm/bootx.h>16#include <asm/btext.h>17#include <asm/io.h>18#include <asm/setup.h>1920#undef DEBUG21#define SET_BOOT_BAT2223#ifdef DEBUG24#define DBG(fmt...) do { bootx_printf(fmt); } while(0)25#else26#define DBG(fmt...) do { } while(0)27#endif2829extern void __start(unsigned long r3, unsigned long r4, unsigned long r5);3031static unsigned long __initdata bootx_dt_strbase;32static unsigned long __initdata bootx_dt_strend;33static unsigned long __initdata bootx_node_chosen;34static boot_infos_t * __initdata bootx_info;35static char __initdata bootx_disp_path[256];3637/* Is boot-info compatible ? */38#define BOOT_INFO_IS_COMPATIBLE(bi) \39((bi)->compatible_version <= BOOT_INFO_VERSION)40#define BOOT_INFO_IS_V2_COMPATIBLE(bi) ((bi)->version >= 2)41#define BOOT_INFO_IS_V4_COMPATIBLE(bi) ((bi)->version >= 4)4243#ifdef CONFIG_BOOTX_TEXT44static void __init bootx_printf(const char *format, ...)45{46const char *p, *q, *s;47va_list args;48unsigned long v;4950va_start(args, format);51for (p = format; *p != 0; p = q) {52for (q = p; *q != 0 && *q != '\n' && *q != '%'; ++q)53;54if (q > p)55btext_drawtext(p, q - p);56if (*q == 0)57break;58if (*q == '\n') {59++q;60btext_flushline();61btext_drawstring("\r\n");62btext_flushline();63continue;64}65++q;66if (*q == 0)67break;68switch (*q) {69case 's':70++q;71s = va_arg(args, const char *);72if (s == NULL)73s = "<NULL>";74btext_drawstring(s);75break;76case 'x':77++q;78v = va_arg(args, unsigned long);79btext_drawhex(v);80break;81}82}83va_end(args);84}85#else /* CONFIG_BOOTX_TEXT */86static void __init bootx_printf(const char *format, ...) {}87#endif /* CONFIG_BOOTX_TEXT */8889static void * __init bootx_early_getprop(unsigned long base,90unsigned long node,91char *prop)92{93struct bootx_dt_node *np = (struct bootx_dt_node *)(base + node);94u32 *ppp = &np->properties;9596while(*ppp) {97struct bootx_dt_prop *pp =98(struct bootx_dt_prop *)(base + *ppp);99100if (strcmp((char *)((unsigned long)pp->name + base),101prop) == 0) {102return (void *)((unsigned long)pp->value + base);103}104ppp = &pp->next;105}106return NULL;107}108109#define dt_push_token(token, mem) \110do { \111*(mem) = ALIGN(*(mem),4); \112*((u32 *)*(mem)) = token; \113*(mem) += 4; \114} while(0)115116static unsigned long __init bootx_dt_find_string(char *str)117{118char *s, *os;119120s = os = (char *)bootx_dt_strbase;121s += 4;122while (s < (char *)bootx_dt_strend) {123if (strcmp(s, str) == 0)124return s - os;125s += strlen(s) + 1;126}127return 0;128}129130static void __init bootx_dt_add_prop(char *name, void *data, int size,131unsigned long *mem_end)132{133unsigned long soff = bootx_dt_find_string(name);134if (data == NULL)135size = 0;136if (soff == 0) {137bootx_printf("WARNING: Can't find string index for <%s>\n",138name);139return;140}141if (size > 0x20000) {142bootx_printf("WARNING: ignoring large property ");143bootx_printf("%s length 0x%x\n", name, size);144return;145}146dt_push_token(OF_DT_PROP, mem_end);147dt_push_token(size, mem_end);148dt_push_token(soff, mem_end);149150/* push property content */151if (size && data) {152memcpy((void *)*mem_end, data, size);153*mem_end = ALIGN(*mem_end + size, 4);154}155}156157static void __init bootx_add_chosen_props(unsigned long base,158unsigned long *mem_end)159{160u32 val;161162bootx_dt_add_prop("linux,bootx", NULL, 0, mem_end);163164if (bootx_info->kernelParamsOffset) {165char *args = (char *)((unsigned long)bootx_info) +166bootx_info->kernelParamsOffset;167bootx_dt_add_prop("bootargs", args, strlen(args) + 1, mem_end);168}169if (bootx_info->ramDisk) {170val = ((unsigned long)bootx_info) + bootx_info->ramDisk;171bootx_dt_add_prop("linux,initrd-start", &val, 4, mem_end);172val += bootx_info->ramDiskSize;173bootx_dt_add_prop("linux,initrd-end", &val, 4, mem_end);174}175if (strlen(bootx_disp_path))176bootx_dt_add_prop("linux,stdout-path", bootx_disp_path,177strlen(bootx_disp_path) + 1, mem_end);178}179180static void __init bootx_add_display_props(unsigned long base,181unsigned long *mem_end,182int has_real_node)183{184boot_infos_t *bi = bootx_info;185u32 tmp;186187if (has_real_node) {188bootx_dt_add_prop("linux,boot-display", NULL, 0, mem_end);189bootx_dt_add_prop("linux,opened", NULL, 0, mem_end);190} else191bootx_dt_add_prop("linux,bootx-noscreen", NULL, 0, mem_end);192193tmp = bi->dispDeviceDepth;194bootx_dt_add_prop("linux,bootx-depth", &tmp, 4, mem_end);195tmp = bi->dispDeviceRect[2] - bi->dispDeviceRect[0];196bootx_dt_add_prop("linux,bootx-width", &tmp, 4, mem_end);197tmp = bi->dispDeviceRect[3] - bi->dispDeviceRect[1];198bootx_dt_add_prop("linux,bootx-height", &tmp, 4, mem_end);199tmp = bi->dispDeviceRowBytes;200bootx_dt_add_prop("linux,bootx-linebytes", &tmp, 4, mem_end);201tmp = (u32)bi->dispDeviceBase;202if (tmp == 0)203tmp = (u32)bi->logicalDisplayBase;204tmp += bi->dispDeviceRect[1] * bi->dispDeviceRowBytes;205tmp += bi->dispDeviceRect[0] * ((bi->dispDeviceDepth + 7) / 8);206bootx_dt_add_prop("linux,bootx-addr", &tmp, 4, mem_end);207}208209static void __init bootx_dt_add_string(char *s, unsigned long *mem_end)210{211unsigned int l = strlen(s) + 1;212memcpy((void *)*mem_end, s, l);213bootx_dt_strend = *mem_end = *mem_end + l;214}215216static void __init bootx_scan_dt_build_strings(unsigned long base,217unsigned long node,218unsigned long *mem_end)219{220struct bootx_dt_node *np = (struct bootx_dt_node *)(base + node);221u32 *cpp, *ppp = &np->properties;222unsigned long soff;223char *namep;224225/* Keep refs to known nodes */226namep = np->full_name ? (char *)(base + np->full_name) : NULL;227if (namep == NULL) {228bootx_printf("Node without a full name !\n");229namep = "";230}231DBG("* strings: %s\n", namep);232233if (!strcmp(namep, "/chosen")) {234DBG(" detected /chosen ! adding properties names !\n");235bootx_dt_add_string("linux,bootx", mem_end);236bootx_dt_add_string("linux,stdout-path", mem_end);237bootx_dt_add_string("linux,initrd-start", mem_end);238bootx_dt_add_string("linux,initrd-end", mem_end);239bootx_dt_add_string("bootargs", mem_end);240bootx_node_chosen = node;241}242if (node == bootx_info->dispDeviceRegEntryOffset) {243DBG(" detected display ! adding properties names !\n");244bootx_dt_add_string("linux,boot-display", mem_end);245bootx_dt_add_string("linux,opened", mem_end);246strscpy(bootx_disp_path, namep, sizeof(bootx_disp_path));247}248249/* get and store all property names */250while (*ppp) {251struct bootx_dt_prop *pp =252(struct bootx_dt_prop *)(base + *ppp);253254namep = pp->name ? (char *)(base + pp->name) : NULL;255if (namep == NULL || strcmp(namep, "name") == 0)256goto next;257/* get/create string entry */258soff = bootx_dt_find_string(namep);259if (soff == 0)260bootx_dt_add_string(namep, mem_end);261next:262ppp = &pp->next;263}264265/* do all our children */266cpp = &np->child;267while(*cpp) {268np = (struct bootx_dt_node *)(base + *cpp);269bootx_scan_dt_build_strings(base, *cpp, mem_end);270cpp = &np->sibling;271}272}273274static void __init bootx_scan_dt_build_struct(unsigned long base,275unsigned long node,276unsigned long *mem_end)277{278struct bootx_dt_node *np = (struct bootx_dt_node *)(base + node);279u32 *cpp, *ppp = &np->properties;280char *namep, *p, *ep, *lp;281int l;282283dt_push_token(OF_DT_BEGIN_NODE, mem_end);284285/* get the node's full name */286namep = np->full_name ? (char *)(base + np->full_name) : NULL;287if (namep == NULL)288namep = "";289l = strlen(namep);290291DBG("* struct: %s\n", namep);292293/* Fixup an Apple bug where they have bogus \0 chars in the294* middle of the path in some properties, and extract295* the unit name (everything after the last '/').296*/297memcpy((void *)*mem_end, namep, l + 1);298namep = (char *)*mem_end;299for (lp = p = namep, ep = namep + l; p < ep; p++) {300if (*p == '/')301lp = namep;302else if (*p != 0)303*lp++ = *p;304}305*lp = 0;306*mem_end = ALIGN((unsigned long)lp + 1, 4);307308/* get and store all properties */309while (*ppp) {310struct bootx_dt_prop *pp =311(struct bootx_dt_prop *)(base + *ppp);312313namep = pp->name ? (char *)(base + pp->name) : NULL;314/* Skip "name" */315if (namep == NULL || !strcmp(namep, "name"))316goto next;317/* Skip "bootargs" in /chosen too as we replace it */318if (node == bootx_node_chosen && !strcmp(namep, "bootargs"))319goto next;320321/* push property head */322bootx_dt_add_prop(namep,323pp->value ? (void *)(base + pp->value): NULL,324pp->length, mem_end);325next:326ppp = &pp->next;327}328329if (node == bootx_node_chosen) {330bootx_add_chosen_props(base, mem_end);331if (bootx_info->dispDeviceRegEntryOffset == 0)332bootx_add_display_props(base, mem_end, 0);333}334else if (node == bootx_info->dispDeviceRegEntryOffset)335bootx_add_display_props(base, mem_end, 1);336337/* do all our children */338cpp = &np->child;339while(*cpp) {340np = (struct bootx_dt_node *)(base + *cpp);341bootx_scan_dt_build_struct(base, *cpp, mem_end);342cpp = &np->sibling;343}344345dt_push_token(OF_DT_END_NODE, mem_end);346}347348static unsigned long __init bootx_flatten_dt(unsigned long start)349{350boot_infos_t *bi = bootx_info;351unsigned long mem_start, mem_end;352struct boot_param_header *hdr;353unsigned long base;354u64 *rsvmap;355356/* Start using memory after the big blob passed by BootX, get357* some space for the header358*/359mem_start = mem_end = ALIGN(((unsigned long)bi) + start, 4);360DBG("Boot params header at: %x\n", mem_start);361hdr = (struct boot_param_header *)mem_start;362mem_end += sizeof(struct boot_param_header);363rsvmap = (u64 *)(ALIGN(mem_end, 8));364hdr->off_mem_rsvmap = ((unsigned long)rsvmap) - mem_start;365mem_end = ((unsigned long)rsvmap) + 8 * sizeof(u64);366367/* Get base of tree */368base = ((unsigned long)bi) + bi->deviceTreeOffset;369370/* Build string array */371DBG("Building string array at: %x\n", mem_end);372DBG("Device Tree Base=%x\n", base);373bootx_dt_strbase = mem_end;374mem_end += 4;375bootx_dt_strend = mem_end;376bootx_scan_dt_build_strings(base, 4, &mem_end);377/* Add some strings */378bootx_dt_add_string("linux,bootx-noscreen", &mem_end);379bootx_dt_add_string("linux,bootx-depth", &mem_end);380bootx_dt_add_string("linux,bootx-width", &mem_end);381bootx_dt_add_string("linux,bootx-height", &mem_end);382bootx_dt_add_string("linux,bootx-linebytes", &mem_end);383bootx_dt_add_string("linux,bootx-addr", &mem_end);384/* Wrap up strings */385hdr->off_dt_strings = bootx_dt_strbase - mem_start;386hdr->dt_strings_size = bootx_dt_strend - bootx_dt_strbase;387388/* Build structure */389mem_end = ALIGN(mem_end, 16);390DBG("Building device tree structure at: %x\n", mem_end);391hdr->off_dt_struct = mem_end - mem_start;392bootx_scan_dt_build_struct(base, 4, &mem_end);393dt_push_token(OF_DT_END, &mem_end);394395/* Finish header */396hdr->boot_cpuid_phys = 0;397hdr->magic = OF_DT_HEADER;398hdr->totalsize = mem_end - mem_start;399hdr->version = OF_DT_VERSION;400/* Version 16 is not backward compatible */401hdr->last_comp_version = 0x10;402403/* Reserve the whole thing and copy the reserve map in, we404* also bump mem_reserve_cnt to cause further reservations to405* fail since it's too late.406*/407mem_end = ALIGN(mem_end, PAGE_SIZE);408DBG("End of boot params: %x\n", mem_end);409rsvmap[0] = mem_start;410rsvmap[1] = mem_end;411if (bootx_info->ramDisk) {412rsvmap[2] = ((unsigned long)bootx_info) + bootx_info->ramDisk;413rsvmap[3] = rsvmap[2] + bootx_info->ramDiskSize;414rsvmap[4] = 0;415rsvmap[5] = 0;416} else {417rsvmap[2] = 0;418rsvmap[3] = 0;419}420421return (unsigned long)hdr;422}423424425#ifdef CONFIG_BOOTX_TEXT426static void __init btext_welcome(boot_infos_t *bi)427{428unsigned long flags;429unsigned long pvr;430431bootx_printf("Welcome to Linux, kernel " UTS_RELEASE "\n");432bootx_printf("\nlinked at : 0x%x", KERNELBASE);433bootx_printf("\nframe buffer at : 0x%x", bi->dispDeviceBase);434bootx_printf(" (phys), 0x%x", bi->logicalDisplayBase);435bootx_printf(" (log)");436bootx_printf("\nklimit : 0x%x",(unsigned long)_end);437bootx_printf("\nboot_info at : 0x%x", bi);438__asm__ __volatile__ ("mfmsr %0" : "=r" (flags));439bootx_printf("\nMSR : 0x%x", flags);440__asm__ __volatile__ ("mfspr %0, 287" : "=r" (pvr));441bootx_printf("\nPVR : 0x%x", pvr);442pvr >>= 16;443if (pvr > 1) {444__asm__ __volatile__ ("mfspr %0, 1008" : "=r" (flags));445bootx_printf("\nHID0 : 0x%x", flags);446}447if (pvr == 8 || pvr == 12 || pvr == 0x800c) {448__asm__ __volatile__ ("mfspr %0, 1019" : "=r" (flags));449bootx_printf("\nICTC : 0x%x", flags);450}451#ifdef DEBUG452bootx_printf("\n\n");453bootx_printf("bi->deviceTreeOffset : 0x%x\n",454bi->deviceTreeOffset);455bootx_printf("bi->deviceTreeSize : 0x%x\n",456bi->deviceTreeSize);457#endif458bootx_printf("\n\n");459}460#endif /* CONFIG_BOOTX_TEXT */461462void __init bootx_init(unsigned long r3, unsigned long r4)463{464boot_infos_t *bi = (boot_infos_t *) r4;465unsigned long hdr;466unsigned long space;467unsigned long ptr;468char *model;469unsigned long offset = reloc_offset();470471reloc_got2(offset);472473bootx_info = bi;474475/* We haven't cleared any bss at this point, make sure476* what we need is initialized477*/478bootx_dt_strbase = bootx_dt_strend = 0;479bootx_node_chosen = 0;480bootx_disp_path[0] = 0;481482if (!BOOT_INFO_IS_V2_COMPATIBLE(bi))483bi->logicalDisplayBase = bi->dispDeviceBase;484485/* Fixup depth 16 -> 15 as that's what MacOS calls 16bpp */486if (bi->dispDeviceDepth == 16)487bi->dispDeviceDepth = 15;488489490#ifdef CONFIG_BOOTX_TEXT491ptr = (unsigned long)bi->logicalDisplayBase;492ptr += bi->dispDeviceRect[1] * bi->dispDeviceRowBytes;493ptr += bi->dispDeviceRect[0] * ((bi->dispDeviceDepth + 7) / 8);494btext_setup_display(bi->dispDeviceRect[2] - bi->dispDeviceRect[0],495bi->dispDeviceRect[3] - bi->dispDeviceRect[1],496bi->dispDeviceDepth, bi->dispDeviceRowBytes,497(unsigned long)bi->logicalDisplayBase);498btext_clearscreen();499btext_flushscreen();500#endif /* CONFIG_BOOTX_TEXT */501502/*503* Test if boot-info is compatible. Done only in config504* CONFIG_BOOTX_TEXT since there is nothing much we can do505* with an incompatible version, except display a message506* and eventually hang the processor...507*508* I'll try to keep enough of boot-info compatible in the509* future to always allow display of this message;510*/511if (!BOOT_INFO_IS_COMPATIBLE(bi)) {512bootx_printf(" !!! WARNING - Incompatible version"513" of BootX !!!\n\n\n");514for (;;)515;516}517if (bi->architecture != BOOT_ARCH_PCI) {518bootx_printf(" !!! WARNING - Unsupported machine"519" architecture !\n");520for (;;)521;522}523524#ifdef CONFIG_BOOTX_TEXT525btext_welcome(bi);526#endif527528/* New BootX enters kernel with MMU off, i/os are not allowed529* here. This hack will have been done by the boostrap anyway.530*/531if (bi->version < 4) {532/*533* XXX If this is an iMac, turn off the USB controller.534*/535model = (char *) bootx_early_getprop(r4 + bi->deviceTreeOffset,5364, "model");537if (model538&& (strcmp(model, "iMac,1") == 0539|| strcmp(model, "PowerMac1,1") == 0)) {540bootx_printf("iMac,1 detected, shutting down USB\n");541out_le32((unsigned __iomem *)0x80880008, 1); /* XXX */542}543}544545/* Get a pointer that points above the device tree, args, ramdisk,546* etc... to use for generating the flattened tree547*/548if (bi->version < 5) {549space = bi->deviceTreeOffset + bi->deviceTreeSize;550if (bi->ramDisk >= space)551space = bi->ramDisk + bi->ramDiskSize;552} else553space = bi->totalParamsSize;554555bootx_printf("Total space used by parameters & ramdisk: 0x%x\n", space);556557/* New BootX will have flushed all TLBs and enters kernel with558* MMU switched OFF, so this should not be useful anymore.559*/560if (bi->version < 4) {561unsigned long x __maybe_unused;562563bootx_printf("Touching pages...\n");564565/*566* Touch each page to make sure the PTEs for them567* are in the hash table - the aim is to try to avoid568* getting DSI exceptions while copying the kernel image.569*/570for (ptr = ((unsigned long) &_stext) & PAGE_MASK;571ptr < (unsigned long)bi + space; ptr += PAGE_SIZE)572x = *(volatile unsigned long *)ptr;573}574575/* Ok, now we need to generate a flattened device-tree to pass576* to the kernel577*/578bootx_printf("Preparing boot params...\n");579580hdr = bootx_flatten_dt(space);581582#ifdef CONFIG_BOOTX_TEXT583#ifdef SET_BOOT_BAT584bootx_printf("Preparing BAT...\n");585btext_prepare_BAT();586#else587btext_unmap();588#endif589#endif590591reloc_got2(-offset);592593__start(hdr, KERNELBASE + offset, 0);594}595596597