/*-1* Copyright (c) 2000 Benno Rice <[email protected]>2* Copyright (c) 2000 Stephane Potvin <[email protected]>3* All rights reserved.4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8* 1. Redistributions of source code must retain the above copyright9* notice, this list of conditions and the following disclaimer.10* 2. Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13*14* THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND15* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE16* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE17* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE18* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL19* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS20* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)21* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT22* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY23* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF24* SUCH DAMAGE.25*/2627#include <sys/endian.h>2829#include <stand.h>30#include "openfirm.h"31#include "libofw.h"32#include "bootstrap.h"3334#include <machine/asm.h>35#include <machine/psl.h>3637#ifdef CAS38static int ppc64_autoload(void);39#endif4041struct arch_switch archsw = { /* MI/MD interface boundary */42.arch_getdev = ofw_getdev,43.arch_copyin = ofw_copyin,44.arch_copyout = ofw_copyout,45.arch_readin = ofw_readin,46#ifdef CAS47.arch_autoload = ppc64_autoload,48#else49.arch_autoload = ofw_autoload,50#endif51};5253uint32_t acells, scells;5455static char bootargs[128];5657#define HEAP_SIZE 0x80000058static char heap[HEAP_SIZE]; // In BSS, so uses no space5960#define OF_puts(fd, text) OF_write(fd, text, strlen(text))6162static __inline register_t63mfmsr(void)64{65register_t value;6667__asm __volatile ("mfmsr %0" : "=r"(value));6869return (value);70}7172void73init_heap(void)74{75bzero(heap, HEAP_SIZE);7677setheap(heap, (void *)((uintptr_t)heap + HEAP_SIZE));78}7980uint64_t81memsize(void)82{83phandle_t memoryp;84cell_t reg[24];85int i, sz;86uint64_t memsz;8788memsz = 0;89memoryp = OF_instance_to_package(memory);9091sz = OF_getencprop(memoryp, "reg", ®[0], sizeof(reg));92sz /= sizeof(reg[0]);9394for (i = 0; i < sz; i += (acells + scells)) {95if (scells > 1)96memsz += (uint64_t)reg[i + acells] << 32;97memsz += reg[i + acells + scells - 1];98}99100return (memsz);101}102103#ifdef CAS104extern int ppc64_cas(void);105106static int107ppc64_autoload(void)108{109const char *cas;110111if ((cas = getenv("cas")) && cas[0] == '1')112if (ppc64_cas() != 0)113return (-1);114return (ofw_autoload());115}116#endif117118#if BYTE_ORDER == LITTLE_ENDIAN119/*120* In Little-endian, we cannot just branch to the client interface. Since121* the client interface is big endian, we have to rfid to it.122* Likewise, when execution resumes, we are in the wrong endianness so123* we must do a fixup before returning to the caller.124*/125static int (*openfirmware_entry)(void *);126extern int openfirmware_trampoline(void *buf, int (*cb)(void *));127128/*129* Wrapper to pass the real entry point to our trampoline.130*/131static int132openfirmware_docall(void *buf)133{134return openfirmware_trampoline(buf, openfirmware_entry);135}136#endif137138int139main(int (*openfirm)(void *))140{141phandle_t root;142int i;143char bootpath[64];144char *ch;145int bargc;146char **bargv;147148/*149* Initialise the Open Firmware routines by giving them the entry point.150*/151#if BYTE_ORDER == LITTLE_ENDIAN152/*153* Use a trampoline entry point for endian fixups.154*/155openfirmware_entry = openfirm;156OF_init(openfirmware_docall);157#else158OF_init(openfirm);159#endif160161root = OF_finddevice("/");162163scells = acells = 1;164OF_getencprop(root, "#address-cells", &acells, sizeof(acells));165OF_getencprop(root, "#size-cells", &scells, sizeof(scells));166167/*168* Initialise the heap as early as possible. Once this is done,169* alloc() is usable. The stack is buried inside us, so this is170* safe.171*/172init_heap();173174/*175* Set up console.176*/177cons_probe();178179#ifdef CAS180setenv("cas", "1", 0);181#endif182183/* Set up currdev variable to have hooks in place. */184env_setenv("currdev", EV_VOLATILE, "", gen_setcurrdev, env_nounset);185186devinit();187188printf("\n%s", bootprog_info);189printf("Memory: %lldKB\n", memsize() / 1024);190191OF_getprop(chosen, "bootpath", bootpath, 64);192ch = strchr(bootpath, ':');193*ch = '\0';194printf("Booted from: %s\n", bootpath);195196printf("\n");197198/*199* Only parse the first bootarg if present. It should200* be simple to handle extra arguments201*/202OF_getprop(chosen, "bootargs", bootargs, sizeof(bootargs));203bargc = 0;204parse(&bargc, &bargv, bootargs);205if (bargc == 1)206env_setenv("currdev", EV_VOLATILE, bargv[0], gen_setcurrdev,207env_nounset);208else209env_setenv("currdev", EV_VOLATILE, bootpath,210gen_setcurrdev, env_nounset);211env_setenv("loaddev", EV_VOLATILE, bootpath, env_noset,212env_nounset);213setenv("LINES", "24", 1); /* optional */214215/*216* On non-Apple hardware, where it works reliably, pass flattened217* device trees to the kernel by default instead of OF CI pointers.218* Apple hardware is the only virtual-mode OF implementation in219* existence, so far as I am aware, so use that as a flag.220*/221if (!(mfmsr() & PSL_DR))222setenv("usefdt", "1", 1);223224interact(); /* doesn't return */225226OF_exit();227228return 0;229}230231COMMAND_SET(halt, "halt", "halt the system", command_halt);232233static int234command_halt(int argc, char *argv[])235{236237OF_exit();238return (CMD_OK);239}240241COMMAND_SET(memmap, "memmap", "print memory map", command_memmap);242243int244command_memmap(int argc, char **argv)245{246247ofw_memmap(acells);248return (CMD_OK);249}250251252