mirror of
https://github.com/drasko/codezero.git
synced 2026-04-17 17:29:04 +02:00
Copying the way source, target files are referred, loader/SConscript now works with new setup.
modified: loader/SConscript new file: loader/main.c deleted: loader/main.c.in
This commit is contained in:
@@ -61,80 +61,73 @@ def ksymToLds(target, source, env):
|
|||||||
.equ %s, %s
|
.equ %s, %s
|
||||||
''' % (symbol, symbol, symbol, convertAddress(address)))
|
''' % (symbol, symbol, symbol, convertAddress(address)))
|
||||||
|
|
||||||
def createKernelSFile(target, source, env):
|
|
||||||
with open(target[0].path, 'w') as asmFile:
|
container_assembler_start = \
|
||||||
asmFile.write('''
|
'''
|
||||||
/*
|
.align 4
|
||||||
* This file is autogenerated.
|
.section .kernel
|
||||||
|
.incbin "%s"
|
||||||
|
'''
|
||||||
|
|
||||||
|
container_assembler_body = \
|
||||||
|
'''
|
||||||
|
.align 4
|
||||||
|
.section .cont.%d
|
||||||
|
.incbin "%s"
|
||||||
|
'''
|
||||||
|
|
||||||
|
container_lds_start = \
|
||||||
|
'''/*
|
||||||
|
* Autogenerated linker script that embeds each container image.
|
||||||
*
|
*
|
||||||
* This file defines kernel symbols extracted from the
|
* Copyright (C) 2009 B Labs
|
||||||
* kernel image in their physical address.
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
.include "%s"
|
SECTIONS
|
||||||
|
{'''
|
||||||
|
|
||||||
.section .kernel
|
container_lds_body = \
|
||||||
|
'''
|
||||||
|
.cont.%d : { *(.cont.%d) }'''
|
||||||
|
|
||||||
.incbin "%s"
|
container_lds_end = \
|
||||||
|
'''
|
||||||
|
}
|
||||||
|
'''
|
||||||
|
|
||||||
''' % (source[0].path , source[1].path))
|
def generate_container_assembler(source, target, env):
|
||||||
for image in source[2:]:
|
with open(target[0].path, "w+") as f:
|
||||||
asmFile.write('''
|
file_body = container_assembler_start % (str(source[0]))
|
||||||
.align 4
|
img_i = 0
|
||||||
.section .%s
|
for img in source[1:]:
|
||||||
.incbin "%s"
|
file_body += container_assembler_body % (img_i, img)
|
||||||
''' % (os.path.splitext(image.name)[0], image.path))
|
img_i += 1
|
||||||
|
|
||||||
def createMainC(target, source, env):
|
f.write(file_body)
|
||||||
with open(source[0].path) as inFile:
|
f.close()
|
||||||
with open(target[0].path, 'w') as outFile:
|
|
||||||
externs = ''
|
|
||||||
declarations = ''
|
|
||||||
loads = ''
|
|
||||||
for item in source[1:]:
|
|
||||||
name = os.path.splitext(item.name)[0]
|
|
||||||
if name == 'start' : name = 'kernel'
|
|
||||||
externs += '''
|
|
||||||
extern char _start_%s[];
|
|
||||||
extern char _end_%s[];
|
|
||||||
''' % (name, name)
|
|
||||||
declarations += ' void *%s_entry = NULL;\n' % (name,)
|
|
||||||
loads += '''
|
|
||||||
printf("Loading the %s...\\n");
|
|
||||||
load_image(&%s_entry, _start_%s, _end_%s);
|
|
||||||
''' %(name, name, name, name)
|
|
||||||
text = inFile.read()
|
|
||||||
text = text.replace('__EXTERNAL_SYMBOLS_EDIT_MARKER__', externs)
|
|
||||||
text = text.replace('__DECLARATIONS_EDIT_MARKER__', declarations)
|
|
||||||
text = text.replace('__LOAD_STATEMENTS_EDIT_MARKER__', loads)
|
|
||||||
outFile.write(text)
|
|
||||||
|
|
||||||
def createLinkerScript(target, source, env):
|
def generate_container_lds(source, target, env):
|
||||||
with open(source[0].path) as inFile:
|
with open(target[0].path, "w+") as f:
|
||||||
with open(target[0].path, 'w') as outFile:
|
img_i = 0
|
||||||
linkerItems = ''
|
file_body = container_lds_start
|
||||||
for item in source[1:]:
|
for img in source[1:]:
|
||||||
name = os.path.splitext(item.name)[0]
|
file_body += container_lds_body % (img_i, img_i)
|
||||||
if name == 'start' : name = 'kernel'
|
img_i += 1
|
||||||
linkerItems += '''
|
file_body += container_lds_end
|
||||||
_start_%s = .;
|
f.write(file_body)
|
||||||
*(.%s)
|
f.close()
|
||||||
_end_%s = .;
|
|
||||||
''' % (name, name, name)
|
|
||||||
outFile.write(inFile.read().replace('__LINKER_ITEMS_EDIT_MARKER__', linkerItems))
|
|
||||||
|
|
||||||
startAxfS = Command('start.axf.S', images[0], ksymToLds)
|
startAxfS = Command('start.axf.S', images[0], ksymToLds)
|
||||||
|
|
||||||
# In the following, it is crucially important that the order of the sources is as it is -- assumptions are
|
# In the following, it is crucially important that the order of the sources is as it is -- assumptions are
|
||||||
# made in the functions doing the processing.
|
# made in the functions doing the processing.
|
||||||
|
|
||||||
kernelS = Command('kernel.S', [startAxfS] + images, createKernelSFile)
|
kernelS = Command('containers.S', images, generate_container_assembler)
|
||||||
mainC = Command('main.c', ['main.c.in'] + images, createMainC)
|
linkerScript = Command('linker.lds', images, generate_container_lds)
|
||||||
linkerScript = Command('linker.lds', ['linker.lds.in'] + images, createLinkerScript)
|
|
||||||
|
|
||||||
## TODO: deal with the situation where there are other .c and .S files in the directory.
|
## TODO: deal with the situation where there are other .c and .S files in the directory.
|
||||||
|
|
||||||
objects = e.Object(['arch.c' , kernelS, startAxfS, mainC])
|
objects = e.Object(['arch.c' , 'main.c', kernelS, startAxfS])
|
||||||
Depends(objects, e['configFiles'])
|
Depends(objects, e['configFiles'])
|
||||||
Depends(objects, images)
|
Depends(objects, images)
|
||||||
program = e.Program('final', objects + [e['baremetal_crt0']])
|
program = e.Program('final', objects + [e['baremetal_crt0']])
|
||||||
|
|||||||
35
loader/main.c
Normal file
35
loader/main.c
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
|
||||||
|
#include <elf/elf.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include "arch.h"
|
||||||
|
|
||||||
|
/* These symbols are defined by the linker scripts. */
|
||||||
|
extern char _start_kernel[];
|
||||||
|
extern char _end_kernel[];
|
||||||
|
extern char _start_containers[];
|
||||||
|
extern char _end_containers[];
|
||||||
|
|
||||||
|
/* This is a kernel symbol exported to loader's linker script from kernel build */
|
||||||
|
extern char bkpt_phys_to_virt[];
|
||||||
|
|
||||||
|
int
|
||||||
|
main(void)
|
||||||
|
{
|
||||||
|
void *kernel_entry = NULL;
|
||||||
|
|
||||||
|
arch_init();
|
||||||
|
|
||||||
|
printf("elf-loader:\tStarted\n");
|
||||||
|
|
||||||
|
printf("Loading the kernel...\n");
|
||||||
|
// load_image(&kernel_entry, _start_kernel, _end_kernel);
|
||||||
|
|
||||||
|
printf("elf-loader:\tkernel entry point is %p\n", kernel_entry);
|
||||||
|
// arch_start_kernel(kernel_entry);
|
||||||
|
|
||||||
|
printf("elf-loader:\tKernel start failed!\n");
|
||||||
|
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
184
loader/main.c.in
184
loader/main.c.in
@@ -1,184 +0,0 @@
|
|||||||
/****
|
|
||||||
**** Template for generating a main.c. Edit markers have replaced some items in the original file
|
|
||||||
****
|
|
||||||
**** Copyright © 2009 B Labs Ltd
|
|
||||||
****
|
|
||||||
**** Author: Russel Winder.
|
|
||||||
****/
|
|
||||||
|
|
||||||
/*******************************************************************************
|
|
||||||
* Filename: src/main.c *
|
|
||||||
* Description: Elf-loader - ELF file kernel/application bootstraper, main *
|
|
||||||
* source file. *
|
|
||||||
* Authors: Adam 'WeirdArms' Wiggins <awiggins@cse.unsw.edu.au>. *
|
|
||||||
* Created: 2004-12-01 *
|
|
||||||
********************************************************************************
|
|
||||||
* *
|
|
||||||
* Australian Public Licence B (OZPLB) *
|
|
||||||
* *
|
|
||||||
* Version 1-0 *
|
|
||||||
* *
|
|
||||||
* Copyright (c) 2004 - 2005 University of New South Wales, Australia *
|
|
||||||
* *
|
|
||||||
* All rights reserved. *
|
|
||||||
* *
|
|
||||||
* Developed by: Operating Systems, Embedded and *
|
|
||||||
* Distributed Systems Group (DiSy) *
|
|
||||||
* University of New South Wales *
|
|
||||||
* http://www.disy.cse.unsw.edu.au *
|
|
||||||
* *
|
|
||||||
* Permission is granted by University of New South Wales, free of charge, to *
|
|
||||||
* any person obtaining a copy of this software and any associated *
|
|
||||||
* documentation files (the "Software") to deal with the Software without *
|
|
||||||
* restriction, including (without limitation) the rights to use, copy, *
|
|
||||||
* modify, adapt, merge, publish, distribute, communicate to the public, *
|
|
||||||
* sublicense, and/or sell, lend or rent out copies of the Software, and *
|
|
||||||
* to permit persons to whom the Software is furnished to do so, subject *
|
|
||||||
* to the following conditions: *
|
|
||||||
* *
|
|
||||||
* * Redistributions of source code must retain the above copyright *
|
|
||||||
* notice, this list of conditions and the following disclaimers. *
|
|
||||||
* *
|
|
||||||
* * Redistributions in binary form must reproduce the above *
|
|
||||||
* copyright notice, this list of conditions and the following *
|
|
||||||
* disclaimers in the documentation and/or other materials provided *
|
|
||||||
* with the distribution. *
|
|
||||||
* *
|
|
||||||
* * Neither the name of University of New South Wales, nor the names of *
|
|
||||||
* its contributors, may be used to endorse or promote products derived *
|
|
||||||
* from this Software without specific prior written permission. *
|
|
||||||
* *
|
|
||||||
* EXCEPT AS EXPRESSLY STATED IN THIS LICENCE AND TO THE FULL EXTENT *
|
|
||||||
* PERMITTED BY APPLICABLE LAW, THE SOFTWARE IS PROVIDED "AS-IS", AND *
|
|
||||||
* NATIONAL ICT AUSTRALIA AND ITS CONTRIBUTORS MAKE NO REPRESENTATIONS, *
|
|
||||||
* WARRANTIES OR CONDITIONS OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING *
|
|
||||||
* BUT NOT LIMITED TO ANY REPRESENTATIONS, WARRANTIES OR CONDITIONS *
|
|
||||||
* REGARDING THE CONTENTS OR ACCURACY OF THE SOFTWARE, OR OF TITLE, *
|
|
||||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NONINFRINGEMENT, *
|
|
||||||
* THE ABSENCE OF LATENT OR OTHER DEFECTS, OR THE PRESENCE OR ABSENCE OF *
|
|
||||||
* ERRORS, WHETHER OR NOT DISCOVERABLE. *
|
|
||||||
* *
|
|
||||||
* TO THE FULL EXTENT PERMITTED BY APPLICABLE LAW, IN NO EVENT SHALL *
|
|
||||||
* NATIONAL ICT AUSTRALIA OR ITS CONTRIBUTORS BE LIABLE ON ANY LEGAL *
|
|
||||||
* THEORY (INCLUDING, WITHOUT LIMITATION, IN AN ACTION OF CONTRACT, *
|
|
||||||
* NEGLIGENCE OR OTHERWISE) FOR ANY CLAIM, LOSS, DAMAGES OR OTHER *
|
|
||||||
* LIABILITY, INCLUDING (WITHOUT LIMITATION) LOSS OF PRODUCTION OR *
|
|
||||||
* OPERATION TIME, LOSS, DAMAGE OR CORRUPTION OF DATA OR RECORDS; OR LOSS *
|
|
||||||
* OF ANTICIPATED SAVINGS, OPPORTUNITY, REVENUE, PROFIT OR GOODWILL, OR *
|
|
||||||
* OTHER ECONOMIC LOSS; OR ANY SPECIAL, INCIDENTAL, INDIRECT, *
|
|
||||||
* CONSEQUENTIAL, PUNITIVE OR EXEMPLARY DAMAGES, ARISING OUT OF OR IN *
|
|
||||||
* CONNECTION WITH THIS LICENCE, THE SOFTWARE OR THE USE OF OR OTHER *
|
|
||||||
* DEALINGS WITH THE SOFTWARE, EVEN IF NATIONAL ICT AUSTRALIA OR ITS *
|
|
||||||
* CONTRIBUTORS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH CLAIM, LOSS, *
|
|
||||||
* DAMAGES OR OTHER LIABILITY. *
|
|
||||||
* *
|
|
||||||
* If applicable legislation implies representations, warranties, or *
|
|
||||||
* conditions, or imposes obligations or liability on University of New South *
|
|
||||||
* Wales or one of its contributors in respect of the Software that *
|
|
||||||
* cannot be wholly or partly excluded, restricted or modified, the *
|
|
||||||
* liability of University of New South Wales or the contributor is limited, to *
|
|
||||||
* the full extent permitted by the applicable legislation, at its *
|
|
||||||
* option, to: *
|
|
||||||
* a. in the case of goods, any one or more of the following: *
|
|
||||||
* i. the replacement of the goods or the supply of equivalent goods; *
|
|
||||||
* ii. the repair of the goods; *
|
|
||||||
* iii. the payment of the cost of replacing the goods or of acquiring *
|
|
||||||
* equivalent goods; *
|
|
||||||
* iv. the payment of the cost of having the goods repaired; or *
|
|
||||||
* b. in the case of services: *
|
|
||||||
* i. the supplying of the services again; or *
|
|
||||||
* ii. the payment of the cost of having the services supplied again. *
|
|
||||||
* *
|
|
||||||
* The construction, validity and performance of this licence is governed *
|
|
||||||
* by the laws in force in New South Wales, Australia. *
|
|
||||||
* *
|
|
||||||
*******************************************************************************/
|
|
||||||
|
|
||||||
#include <elf/elf.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include "arch.h"
|
|
||||||
|
|
||||||
/*****************
|
|
||||||
* Extern symbols *
|
|
||||||
*****************/
|
|
||||||
|
|
||||||
/* Variable declarations added here for symbols defined by the linker scripts. */
|
|
||||||
|
|
||||||
__EXTERNAL_SYMBOLS_EDIT_MARKER__
|
|
||||||
|
|
||||||
/* This is a kernel symbol exported to loader's linker script from kernel build */
|
|
||||||
extern char bkpt_phys_to_virt[];
|
|
||||||
|
|
||||||
/*****************************
|
|
||||||
* Local function prototypes. *
|
|
||||||
*****************************/
|
|
||||||
|
|
||||||
static void load_image(void **entry, char *start, char *end);
|
|
||||||
|
|
||||||
int
|
|
||||||
main(void)
|
|
||||||
{
|
|
||||||
/* Declarations added here.*/
|
|
||||||
|
|
||||||
__DECLARATIONS_EDIT_MARKER__
|
|
||||||
|
|
||||||
arch_init();
|
|
||||||
|
|
||||||
printf("elf-loader:\tStarted\n");
|
|
||||||
|
|
||||||
/* Loader statements added here. */
|
|
||||||
|
|
||||||
__LOAD_STATEMENTS_EDIT_MARKER__
|
|
||||||
|
|
||||||
printf("elf-loader:\tkernel entry point is %p\n", kernel_entry);
|
|
||||||
arch_start_kernel(kernel_entry);
|
|
||||||
|
|
||||||
printf("elf-loader:\tKernel start failed!\n");
|
|
||||||
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*******************
|
|
||||||
* Local functions. *
|
|
||||||
*******************/
|
|
||||||
|
|
||||||
static void
|
|
||||||
load_image(void** entry, char *start, char *end)
|
|
||||||
{
|
|
||||||
if(start == end) {
|
|
||||||
printf("elf-loader: error, dite image is empty!\n");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
/* awiggins (2004-12-21): Should probably add an alignment check? */
|
|
||||||
/** awiggins (2005-05-29): Probably should add checks to see
|
|
||||||
* the correct kind of elf file is being loaded for the platform,
|
|
||||||
* or does the elf library do that somewhere?.
|
|
||||||
*/
|
|
||||||
if(!elf32_checkFile((struct Elf32_Header*)start)) {
|
|
||||||
// printf("32-bit elf image detected.\n");
|
|
||||||
*entry = (void*)(uintptr_t)
|
|
||||||
elf32_getEntryPoint((struct Elf32_Header*)start);
|
|
||||||
printf("Entry point: %p\n", *entry);
|
|
||||||
} else if(!elf64_checkFile((struct Elf64_Header*)start)) {
|
|
||||||
*entry =
|
|
||||||
(void*)(uintptr_t)elf64_getEntryPoint((struct Elf64_Header*)start);
|
|
||||||
} else {
|
|
||||||
printf("elf-loader: error, dite image not a valid elf file!\n");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
#ifdef __PPC64__
|
|
||||||
if (arch_claim_memory((struct Elf32_Header*)start)) {
|
|
||||||
printf("could not claim memory for elf file!\n");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
//printf("Start: %p\n", start);
|
|
||||||
//printf("End: %p\n", end);
|
|
||||||
|
|
||||||
if(!elf_loadFile(start, true)) {
|
|
||||||
printf("elf-loader: error, unable to load dite image!\n");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Reference in New Issue
Block a user