/branches/dd/boot/genarch/include/softint |
---|
0,0 → 1,0 |
link ../../../kernel/genarch/include/softint |
Property changes: |
Added: svn:special |
+* |
\ No newline at end of property |
/branches/dd/boot/genarch/division.c |
---|
0,0 → 1,0 |
link ../../kernel/genarch/src/softint/division.c |
Property changes: |
Added: svn:special |
+* |
\ No newline at end of property |
/branches/dd/boot/genarch/ofw.h |
---|
0,0 → 1,128 |
/* |
* Copyright (c) 2005 Martin Decky |
* All rights reserved. |
* |
* Redistribution and use in source and binary forms, with or without |
* modification, are permitted provided that the following conditions |
* are met: |
* |
* - Redistributions of source code must retain the above copyright |
* notice, this list of conditions and the following disclaimer. |
* - Redistributions in binary form must reproduce the above copyright |
* notice, this list of conditions and the following disclaimer in the |
* documentation and/or other materials provided with the distribution. |
* - The name of the author may not be used to endorse or promote products |
* derived from this software without specific prior written permission. |
* |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
*/ |
#ifndef BOOT_OFW_H_ |
#define BOOT_OFW_H_ |
#include <types.h> |
#include <stdarg.h> |
#define BUF_SIZE 1024 |
#define MEMMAP_MAX_RECORDS 32 |
#define MAX_OFW_ARGS 12 |
typedef unative_t ofw_arg_t; |
typedef unsigned int ihandle; |
typedef unsigned int phandle; |
/** OpenFirmware command structure |
* |
*/ |
typedef struct { |
ofw_arg_t service; /**< Command name. */ |
ofw_arg_t nargs; /**< Number of in arguments. */ |
ofw_arg_t nret; /**< Number of out arguments. */ |
ofw_arg_t args[MAX_OFW_ARGS]; /**< List of arguments. */ |
} ofw_args_t; |
typedef struct { |
void *start; |
uint32_t size; |
} memzone_t; |
typedef struct { |
uint32_t total; |
uint32_t count; |
memzone_t zones[MEMMAP_MAX_RECORDS]; |
} memmap_t; |
typedef struct { |
void *addr; |
uint32_t width; |
uint32_t height; |
uint32_t bpp; |
uint32_t scanline; |
} screen_t; |
typedef struct { |
void *addr; |
uint32_t size; |
} keyboard_t; |
typedef struct { |
uint32_t info; |
uint32_t addr_hi; |
uint32_t addr_lo; |
} pci_addr_t; |
typedef struct { |
pci_addr_t addr; |
uint32_t size_hi; |
uint32_t size_lo; |
} pci_reg_t; |
extern uintptr_t ofw_cif; |
extern phandle ofw_chosen; |
extern ihandle ofw_stdout; |
extern phandle ofw_root; |
extern ihandle ofw_mmu; |
extern phandle ofw_memory; |
extern phandle ofw_aliases; |
extern void ofw_init(void); |
extern void ofw_write(const char *str, const int len); |
extern int ofw_get_property(const phandle device, const char *name, void *buf, const int buflen); |
extern int ofw_get_proplen(const phandle device, const char *name); |
extern int ofw_next_property(const phandle device, char *previous, char *buf); |
extern phandle ofw_get_child_node(const phandle node); |
extern phandle ofw_get_peer_node(const phandle node); |
extern phandle ofw_find_device(const char *name); |
extern int ofw_package_to_path(const phandle device, char *buf, const int buflen); |
extern int ofw(ofw_args_t *arg); |
extern unsigned long ofw_call(const char *service, const int nargs, const int nret, ofw_arg_t *rets, ...); |
extern unsigned int ofw_get_address_cells(const phandle device); |
extern unsigned int ofw_get_size_cells(const phandle device); |
extern void *ofw_translate(const void *virt); |
extern int ofw_translate_failed(ofw_arg_t flag); |
extern void *ofw_claim_virt(const void *virt, const int len); |
extern void *ofw_claim_phys(const void *virt, const int len); |
extern int ofw_map(const void *phys, const void *virt, const int size, const int mode); |
extern int ofw_memmap(memmap_t *map); |
extern int ofw_screen(screen_t *screen); |
extern int ofw_keyboard(keyboard_t *keyboard); |
extern void ofw_quiesce(void); |
#endif |
/branches/dd/boot/genarch/balloc.h |
---|
0,0 → 1,44 |
/* |
* Copyright (c) 2006 Jakub Jermar |
* All rights reserved. |
* |
* Redistribution and use in source and binary forms, with or without |
* modification, are permitted provided that the following conditions |
* are met: |
* |
* - Redistributions of source code must retain the above copyright |
* notice, this list of conditions and the following disclaimer. |
* - Redistributions in binary form must reproduce the above copyright |
* notice, this list of conditions and the following disclaimer in the |
* documentation and/or other materials provided with the distribution. |
* - The name of the author may not be used to endorse or promote products |
* derived from this software without specific prior written permission. |
* |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
*/ |
#ifndef BOOT_BALLOC_H_ |
#define BOOT_BALLOC_H_ |
#include <types.h> |
#define BALLOC_MAX_SIZE (1024 * 1024) |
typedef struct { |
uintptr_t base; |
size_t size; |
} ballocs_t; |
extern void balloc_init(ballocs_t *b, uintptr_t base); |
extern void *balloc(size_t size, size_t alignment); |
#endif |
/branches/dd/boot/genarch/ofw.c |
---|
0,0 → 1,348 |
/* |
* Copyright (c) 2005 Martin Decky |
* All rights reserved. |
* |
* Redistribution and use in source and binary forms, with or without |
* modification, are permitted provided that the following conditions |
* are met: |
* |
* - Redistributions of source code must retain the above copyright |
* notice, this list of conditions and the following disclaimer. |
* - Redistributions in binary form must reproduce the above copyright |
* notice, this list of conditions and the following disclaimer in the |
* documentation and/or other materials provided with the distribution. |
* - The name of the author may not be used to endorse or promote products |
* derived from this software without specific prior written permission. |
* |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
*/ |
#include <ofw.h> |
#include <ofwarch.h> |
#include <printf.h> |
#include <asm.h> |
#include <types.h> |
uintptr_t ofw_cif; |
phandle ofw_chosen; |
ihandle ofw_stdout; |
phandle ofw_root; |
ihandle ofw_mmu; |
ihandle ofw_memory_prop; |
phandle ofw_memory; |
phandle ofw_aliases; |
void ofw_init(void) |
{ |
ofw_chosen = ofw_find_device("/chosen"); |
if (ofw_chosen == -1) |
halt(); |
if (ofw_get_property(ofw_chosen, "stdout", &ofw_stdout, sizeof(ofw_stdout)) <= 0) |
ofw_stdout = 0; |
ofw_root = ofw_find_device("/"); |
if (ofw_root == -1) { |
puts("\r\nError: Unable to find / device, halted.\r\n"); |
halt(); |
} |
if (ofw_get_property(ofw_chosen, "mmu", &ofw_mmu, sizeof(ofw_mmu)) <= 0) { |
puts("\r\nError: Unable to get mmu property, halted.\r\n"); |
halt(); |
} |
if (ofw_get_property(ofw_chosen, "memory", &ofw_memory_prop, sizeof(ofw_memory_prop)) <= 0) { |
puts("\r\nError: Unable to get memory property, halted.\r\n"); |
halt(); |
} |
ofw_memory = ofw_find_device("/memory"); |
if (ofw_memory == -1) { |
puts("\r\nError: Unable to find /memory device, halted.\r\n"); |
halt(); |
} |
ofw_aliases = ofw_find_device("/aliases"); |
if (ofw_aliases == -1) { |
puts("\r\nError: Unable to find /aliases device, halted.\r\n"); |
halt(); |
} |
} |
/** Perform a call to OpenFirmware client interface. |
* |
* @param service String identifying the service requested. |
* @param nargs Number of input arguments. |
* @param nret Number of output arguments. This includes the return value. |
* @param rets Buffer for output arguments or NULL. The buffer must accommodate nret - 1 items. |
* |
* @return Return value returned by the client interface. |
*/ |
unsigned long ofw_call(const char *service, const int nargs, const int nret, ofw_arg_t *rets, ...) |
{ |
va_list list; |
ofw_args_t args; |
int i; |
args.service = (ofw_arg_t) service; |
args.nargs = nargs; |
args.nret = nret; |
va_start(list, rets); |
for (i = 0; i < nargs; i++) |
args.args[i] = va_arg(list, ofw_arg_t); |
va_end(list); |
for (i = 0; i < nret; i++) |
args.args[i + nargs] = 0; |
(void) ofw(&args); |
for (i = 1; i < nret; i++) |
rets[i - 1] = args.args[i + nargs]; |
return args.args[nargs]; |
} |
phandle ofw_find_device(const char *name) |
{ |
return ofw_call("finddevice", 1, 1, NULL, name); |
} |
int ofw_get_property(const phandle device, const char *name, void *buf, const int buflen) |
{ |
return ofw_call("getprop", 4, 1, NULL, device, name, buf, buflen); |
} |
int ofw_get_proplen(const phandle device, const char *name) |
{ |
return ofw_call("getproplen", 2, 1, NULL, device, name); |
} |
int ofw_next_property(const phandle device, char *previous, char *buf) |
{ |
return ofw_call("nextprop", 3, 1, NULL, device, previous, buf); |
} |
int ofw_package_to_path(const phandle device, char *buf, const int buflen) |
{ |
return ofw_call("package-to-path", 3, 1, NULL, device, buf, buflen); |
} |
unsigned int ofw_get_address_cells(const phandle device) |
{ |
unsigned int ret = 1; |
if (ofw_get_property(device, "#address-cells", &ret, sizeof(ret)) <= 0) |
if (ofw_get_property(ofw_root, "#address-cells", &ret, sizeof(ret)) <= 0) |
ret = OFW_ADDRESS_CELLS; |
return ret; |
} |
unsigned int ofw_get_size_cells(const phandle device) |
{ |
unsigned int ret; |
if (ofw_get_property(device, "#size-cells", &ret, sizeof(ret)) <= 0) |
if (ofw_get_property(ofw_root, "#size-cells", &ret, sizeof(ret)) <= 0) |
ret = OFW_SIZE_CELLS; |
return ret; |
} |
phandle ofw_get_child_node(const phandle node) |
{ |
return ofw_call("child", 1, 1, NULL, node); |
} |
phandle ofw_get_peer_node(const phandle node) |
{ |
return ofw_call("peer", 1, 1, NULL, node); |
} |
static ihandle ofw_open(const char *name) |
{ |
return ofw_call("open", 1, 1, NULL, name); |
} |
void ofw_write(const char *str, const int len) |
{ |
if (ofw_stdout == 0) |
return; |
ofw_call("write", 3, 1, NULL, ofw_stdout, str, len); |
} |
void *ofw_translate(const void *virt) |
{ |
ofw_arg_t result[4]; |
int shift; |
if (ofw_call("call-method", 3, 5, result, "translate", ofw_mmu, virt) != 0) { |
puts("Error: MMU method translate() failed, halting.\n"); |
halt(); |
} |
if (ofw_translate_failed(result[0])) |
return NULL; |
if (sizeof(unative_t) == 8) |
shift = 32; |
else |
shift = 0; |
return (void *) ((result[2] << shift) | result[3]); |
} |
void *ofw_claim_virt(const void *virt, const int len) |
{ |
ofw_arg_t retaddr; |
if (ofw_call("call-method", 5, 2, &retaddr, "claim", ofw_mmu, 0, len, virt) != 0) { |
puts("Error: MMU method claim() failed, halting.\n"); |
halt(); |
} |
return (void *) retaddr; |
} |
void *ofw_claim_phys(const void *phys, const int len) |
{ |
ofw_arg_t retaddr[2]; |
int shift; |
if (sizeof(unative_t) == 8) { |
shift = 32; |
if (ofw_call("call-method", 6, 3, retaddr, "claim", |
ofw_memory_prop, 0, len, ((uintptr_t) phys) >> shift, |
((uintptr_t) phys) & ((uint32_t) -1)) != 0) { |
/* |
* Note that this will help us to discover |
* conflicts between OpenFirmware allocations |
* and our use of physical memory. |
* It is better to detect collisions here |
* than to cope with weird errors later. |
* |
* So this is really not to make the loader |
* more generic; it is here for debugging |
* purposes. |
*/ |
puts("Error: memory method claim() failed, halting.\n"); |
halt(); |
} |
} else { |
shift = 0; |
/* |
* FIXME: the number of arguments is probably different... |
*/ |
puts("Error: 32-bit ofw_claim_phys not implemented.\n"); |
halt(); |
} |
return (void *) ((retaddr[0] << shift) | retaddr[1]); |
} |
int ofw_map(const void *phys, const void *virt, const int size, const int mode) |
{ |
uintptr_t phys_hi, phys_lo; |
if (sizeof(unative_t) == 8) { |
int shift = 32; |
phys_hi = (uintptr_t) phys >> shift; |
phys_lo = (uintptr_t) phys & 0xffffffff; |
} else { |
phys_hi = 0; |
phys_lo = (uintptr_t) phys; |
} |
return ofw_call("call-method", 7, 1, NULL, "map", ofw_mmu, mode, size, virt, |
phys_hi, phys_lo); |
} |
/** Save OpenFirmware physical memory map. |
* |
* @param map Memory map structure where the map will be saved. |
* |
* @return Zero on failure, non-zero on success. |
*/ |
int ofw_memmap(memmap_t *map) |
{ |
unsigned int ac = ofw_get_address_cells(ofw_memory); |
unsigned int sc = ofw_get_size_cells(ofw_memory); |
uint32_t buf[((ac + sc) * MEMMAP_MAX_RECORDS)]; |
int ret = ofw_get_property(ofw_memory, "reg", buf, sizeof(buf)); |
if (ret <= 0) /* ret is the number of written bytes */ |
return false; |
int pos; |
map->total = 0; |
map->count = 0; |
for (pos = 0; (pos < ret / sizeof(uint32_t)) && |
(map->count < MEMMAP_MAX_RECORDS); pos += ac + sc) { |
void * start = (void *) ((uintptr_t) buf[pos + ac - 1]); |
unsigned int size = buf[pos + ac + sc - 1]; |
if (size > 0) { |
map->zones[map->count].start = start; |
map->zones[map->count].size = size; |
map->count++; |
map->total += size; |
} |
} |
return true; |
} |
int ofw_screen(screen_t *screen) |
{ |
char device_name[BUF_SIZE]; |
uint32_t virtaddr; |
if (ofw_get_property(ofw_aliases, "screen", device_name, sizeof(device_name)) <= 0) |
return false; |
phandle device = ofw_find_device(device_name); |
if (device == -1) |
return false; |
if (ofw_get_property(device, "address", &virtaddr, sizeof(virtaddr)) <= 0) |
return false; |
screen->addr = (void *) ((uintptr_t) virtaddr); |
if (ofw_get_property(device, "width", &screen->width, sizeof(screen->width)) <= 0) |
return false; |
if (ofw_get_property(device, "height", &screen->height, sizeof(screen->height)) <= 0) |
return false; |
if (ofw_get_property(device, "depth", &screen->bpp, sizeof(screen->bpp)) <= 0) |
return false; |
if (ofw_get_property(device, "linebytes", &screen->scanline, sizeof(screen->scanline)) <= 0) |
return false; |
return true; |
} |
void ofw_quiesce(void) |
{ |
ofw_call("quiesce", 0, 0, NULL); |
} |
/branches/dd/boot/genarch/balloc.c |
---|
0,0 → 1,57 |
/* |
* Copyright (c) 2006 Jakub Jermar |
* All rights reserved. |
* |
* Redistribution and use in source and binary forms, with or without |
* modification, are permitted provided that the following conditions |
* are met: |
* |
* - Redistributions of source code must retain the above copyright |
* notice, this list of conditions and the following disclaimer. |
* - Redistributions in binary form must reproduce the above copyright |
* notice, this list of conditions and the following disclaimer in the |
* documentation and/or other materials provided with the distribution. |
* - The name of the author may not be used to endorse or promote products |
* derived from this software without specific prior written permission. |
* |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
*/ |
#include <balloc.h> |
#include <types.h> |
#include <align.h> |
static ballocs_t *ballocs; |
void balloc_init(ballocs_t *b, uintptr_t base) |
{ |
ballocs = b; |
ballocs->base = base; |
ballocs->size = 0; |
} |
void *balloc(size_t size, size_t alignment) |
{ |
uintptr_t addr; |
/* Enforce minimal alignment. */ |
alignment = ALIGN_UP(alignment, 4); |
addr = ballocs->base + ALIGN_UP(ballocs->size, alignment); |
if (ALIGN_UP(ballocs->size, alignment) + size > BALLOC_MAX_SIZE) |
return NULL; |
ballocs->size = ALIGN_UP(ballocs->size, alignment) + size; |
return (void *) addr; |
} |
/branches/dd/boot/genarch/ofw_tree.h |
---|
0,0 → 1,65 |
/* |
* Copyright (c) 2006 Jakub Jermar |
* All rights reserved. |
* |
* Redistribution and use in source and binary forms, with or without |
* modification, are permitted provided that the following conditions |
* are met: |
* |
* - Redistributions of source code must retain the above copyright |
* notice, this list of conditions and the following disclaimer. |
* - Redistributions in binary form must reproduce the above copyright |
* notice, this list of conditions and the following disclaimer in the |
* documentation and/or other materials provided with the distribution. |
* - The name of the author may not be used to endorse or promote products |
* derived from this software without specific prior written permission. |
* |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
*/ |
#ifndef BOOT_OFW_TREE_H_ |
#define BOOT_OFW_TREE_H_ |
#include <types.h> |
#include <ofw.h> |
#define OFW_TREE_PROPERTY_MAX_NAMELEN 32 |
typedef struct ofw_tree_node ofw_tree_node_t; |
typedef struct ofw_tree_property ofw_tree_property_t; |
/** Memory representation of OpenFirmware device tree node. */ |
struct ofw_tree_node { |
ofw_tree_node_t *parent; |
ofw_tree_node_t *peer; |
ofw_tree_node_t *child; |
uint32_t node_handle; /**< Old OpenFirmware node handle. */ |
char *da_name; /**< Disambigued name. */ |
unsigned properties; /**< Number of properties. */ |
ofw_tree_property_t *property; |
void *device; /**< Member used solely by the kernel. */ |
}; |
/** Memory representation of OpenFirmware device tree node property. */ |
struct ofw_tree_property { |
char name[OFW_TREE_PROPERTY_MAX_NAMELEN]; |
size_t size; |
void *value; |
}; |
extern ofw_tree_node_t *ofw_tree_build(void); |
#endif |
/branches/dd/boot/genarch/ofw_tree.c |
---|
0,0 → 1,228 |
/* |
* Copyright (c) 2006 Jakub Jermar |
* All rights reserved. |
* |
* Redistribution and use in source and binary forms, with or without |
* modification, are permitted provided that the following conditions |
* are met: |
* |
* - Redistributions of source code must retain the above copyright |
* notice, this list of conditions and the following disclaimer. |
* - Redistributions in binary form must reproduce the above copyright |
* notice, this list of conditions and the following disclaimer in the |
* documentation and/or other materials provided with the distribution. |
* - The name of the author may not be used to endorse or promote products |
* derived from this software without specific prior written permission. |
* |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
*/ |
#include <ofw_tree.h> |
#include <ofw.h> |
#include <types.h> |
#include <string.h> |
#include <balloc.h> |
#include <asm.h> |
#define MAX_PATH_LEN 256 |
static ofw_tree_node_t *ofw_tree_node_alloc(void) |
{ |
return balloc(sizeof(ofw_tree_node_t), sizeof(ofw_tree_node_t)); |
} |
static ofw_tree_property_t *ofw_tree_properties_alloc(unsigned count) |
{ |
return balloc(count * sizeof(ofw_tree_property_t), sizeof(ofw_tree_property_t)); |
} |
static void * ofw_tree_space_alloc(size_t size) |
{ |
char *addr; |
/* |
* What we do here is a nasty hack :-) |
* Problem: string property values that are allocated via this |
* function typically do not contain the trailing '\0'. This |
* is very uncomfortable for kernel, which is supposed to deal |
* with the properties. |
* Solution: when allocating space via this function, we always |
* allocate space for the extra '\0' character that we store |
* behind the requested memory. |
*/ |
addr = balloc(size + 1, size); |
if (addr) |
addr[size] = '\0'; |
return addr; |
} |
/** Transfer information from one OpenFirmware node into its memory representation. |
* |
* Transfer entire information from the OpenFirmware device tree 'current' node to |
* its memory representation in 'current_node'. This function recursively processes |
* all node's children. Node's peers are processed iteratively in order to prevent |
* stack from overflowing. |
* |
* @param current_node Pointer to uninitialized ofw_tree_node structure that will |
* become the memory represenation of 'current'. |
* @param parent_node Parent ofw_tree_node structure or NULL in case of root node. |
* @param current OpenFirmware phandle to the current device tree node. |
*/ |
static void ofw_tree_node_process(ofw_tree_node_t *current_node, |
ofw_tree_node_t *parent_node, phandle current) |
{ |
static char path[MAX_PATH_LEN+1]; |
static char name[OFW_TREE_PROPERTY_MAX_NAMELEN]; |
phandle peer; |
phandle child; |
size_t len; |
int i; |
while (current_node) { |
/* |
* Initialize node. |
*/ |
current_node->parent = parent_node; |
current_node->peer = NULL; |
current_node->child = NULL; |
current_node->node_handle = current; |
current_node->properties = 0; |
current_node->property = NULL; |
current_node->device = NULL; |
/* |
* Get the disambigued name. |
*/ |
len = ofw_package_to_path(current, path, MAX_PATH_LEN); |
if (len == -1) |
return; |
path[len] = '\0'; |
for (i = len - 1; i >= 0 && path[i] != '/'; i--) |
; |
i++; /* do not include '/' */ |
len -= i; |
/* add space for trailing '\0' */ |
current_node->da_name = ofw_tree_space_alloc(len + 1); |
if (!current_node->da_name) |
return; |
memcpy(current_node->da_name, &path[i], len); |
current_node->da_name[len] = '\0'; |
/* |
* Recursively process the potential child node. |
*/ |
child = ofw_get_child_node(current); |
if (child != 0 && child != -1) { |
ofw_tree_node_t *child_node; |
child_node = ofw_tree_node_alloc(); |
if (child_node) { |
ofw_tree_node_process(child_node, current_node, child); |
current_node->child = child_node; |
} |
} |
/* |
* Count properties. |
*/ |
name[0] = '\0'; |
while (ofw_next_property(current, name, name) == 1) |
current_node->properties++; |
if (!current_node->properties) |
return; |
/* |
* Copy properties. |
*/ |
current_node->property = ofw_tree_properties_alloc(current_node->properties); |
if (!current_node->property) |
return; |
name[0] = '\0'; |
for (i = 0; ofw_next_property(current, name, name) == 1; i++) { |
size_t size; |
if (i == current_node->properties) |
break; |
memcpy(current_node->property[i].name, name, |
OFW_TREE_PROPERTY_MAX_NAMELEN); |
current_node->property[i].name[OFW_TREE_PROPERTY_MAX_NAMELEN] = '\0'; |
size = ofw_get_proplen(current, name); |
current_node->property[i].size = size; |
if (size) { |
void *buf; |
buf = ofw_tree_space_alloc(size); |
if (current_node->property[i].value = buf) { |
/* |
* Copy property value to memory node. |
*/ |
(void) ofw_get_property(current, name, buf, size); |
} |
} else { |
current_node->property[i].value = NULL; |
} |
} |
current_node->properties = i; /* Just in case we ran out of memory. */ |
/* |
* Iteratively process the next peer node. |
* Note that recursion is a bad idea here. |
* Due to the topology of the OpenFirmware device tree, |
* the nesting of peer nodes could be to wide and the |
* risk of overflowing the stack is too real. |
*/ |
peer = ofw_get_peer_node(current); |
if (peer != 0 && peer != -1) { |
ofw_tree_node_t *peer_node; |
peer_node = ofw_tree_node_alloc(); |
if (peer_node) { |
current_node->peer = peer_node; |
current_node = peer_node; |
current = peer; |
/* |
* Process the peer in next iteration. |
*/ |
continue; |
} |
} |
/* |
* No more peers on this level. |
*/ |
break; |
} |
} |
/** Construct memory representation of OpenFirmware device tree. |
* |
* @return NULL on failure or pointer to the root node. |
*/ |
ofw_tree_node_t *ofw_tree_build(void) |
{ |
ofw_tree_node_t *root; |
root = ofw_tree_node_alloc(); |
if (root) |
ofw_tree_node_process(root, NULL, ofw_root); |
return root; |
} |