| 1 |
2 |
drasko |
#! /usr/bin/env python2.6
|
| 2 |
|
|
# -*- mode: python; coding: utf-8; -*-
|
| 3 |
|
|
#
|
| 4 |
|
|
# Codezero -- a microkernel for embedded systems.
|
| 5 |
|
|
#
|
| 6 |
|
|
# Copyright © 2009 B Labs Ltd
|
| 7 |
|
|
#
|
| 8 |
|
|
import os, sys, shelve, glob
|
| 9 |
|
|
from os.path import join
|
| 10 |
|
|
|
| 11 |
|
|
PROJRELROOT = '../../'
|
| 12 |
|
|
|
| 13 |
|
|
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), PROJRELROOT)))
|
| 14 |
|
|
sys.path.append(os.path.abspath('../'))
|
| 15 |
|
|
|
| 16 |
|
|
from config.projpaths import *
|
| 17 |
|
|
from config.configuration import *
|
| 18 |
|
|
|
| 19 |
|
|
container_assembler_body = \
|
| 20 |
|
|
'''
|
| 21 |
|
|
.align 4
|
| 22 |
|
|
.section .img.%d
|
| 23 |
|
|
.incbin "%s"
|
| 24 |
|
|
'''
|
| 25 |
|
|
|
| 26 |
|
|
container_lds_start = \
|
| 27 |
|
|
'''/*
|
| 28 |
|
|
* Autogenerated linker script that embeds each image to be
|
| 29 |
|
|
* placed in a single container.
|
| 30 |
|
|
*
|
| 31 |
|
|
* Copyright (C) 2009 B Labs
|
| 32 |
|
|
*/
|
| 33 |
|
|
|
| 34 |
|
|
SECTIONS
|
| 35 |
|
|
{'''
|
| 36 |
|
|
|
| 37 |
|
|
container_lds_body = \
|
| 38 |
|
|
'''
|
| 39 |
|
|
.img.%d : { *(.img.%d) }'''
|
| 40 |
|
|
|
| 41 |
|
|
container_lds_end = \
|
| 42 |
|
|
'''
|
| 43 |
|
|
}
|
| 44 |
|
|
'''
|
| 45 |
|
|
|
| 46 |
|
|
# Create container build base as:
|
| 47 |
|
|
# conts/linux -> build/cont[0-9]
|
| 48 |
|
|
def source_to_builddir(srcdir, id):
|
| 49 |
|
|
cont_builddir = \
|
| 50 |
|
|
os.path.relpath(srcdir, \
|
| 51 |
|
|
PROJROOT).replace("conts", \
|
| 52 |
|
|
"cont" + str(id))
|
| 53 |
|
|
return join(BUILDDIR, cont_builddir)
|
| 54 |
|
|
|
| 55 |
|
|
class LinuxContainerPacker:
|
| 56 |
|
|
def __init__(self, container, linux_builder, rootfs_builder, atags_builder):
|
| 57 |
|
|
|
| 58 |
|
|
# Here, we simply attempt to get PROJROOT/conts as
|
| 59 |
|
|
# PROJROOT/build/cont[0-9]
|
| 60 |
|
|
self.CONTAINER_BUILDDIR_BASE = \
|
| 61 |
|
|
source_to_builddir(join(PROJROOT,'conts'), container.id)
|
| 62 |
|
|
|
| 63 |
|
|
self.container_lds_out = join(self.CONTAINER_BUILDDIR_BASE, \
|
| 64 |
|
|
'container.lds')
|
| 65 |
|
|
self.container_S_out = join(self.CONTAINER_BUILDDIR_BASE, 'container.S')
|
| 66 |
|
|
self.container_elf_out = join(self.CONTAINER_BUILDDIR_BASE, \
|
| 67 |
|
|
'container' + str(container.id) + '.elf')
|
| 68 |
|
|
self.kernel_image_in = linux_builder.kernel_image
|
| 69 |
|
|
self.rootfs_elf_in = rootfs_builder.rootfs_elf_out
|
| 70 |
|
|
self.atags_elf_in = atags_builder.atags_elf_out
|
| 71 |
|
|
|
| 72 |
|
|
def generate_container_assembler(self, source):
|
| 73 |
|
|
with open(self.container_S_out, 'w+') as f:
|
| 74 |
|
|
file_body = ""
|
| 75 |
|
|
img_i = 0
|
| 76 |
|
|
for img in source:
|
| 77 |
|
|
file_body += container_assembler_body % (img_i, img)
|
| 78 |
|
|
img_i += 1
|
| 79 |
|
|
|
| 80 |
|
|
f.write(file_body)
|
| 81 |
|
|
f.close()
|
| 82 |
|
|
|
| 83 |
|
|
def generate_container_lds(self, source):
|
| 84 |
|
|
with open(self.container_lds_out, 'w+') as f:
|
| 85 |
|
|
img_i = 0
|
| 86 |
|
|
file_body = container_lds_start
|
| 87 |
|
|
for img in source:
|
| 88 |
|
|
file_body += container_lds_body % (img_i, img_i)
|
| 89 |
|
|
img_i += 1
|
| 90 |
|
|
file_body += container_lds_end
|
| 91 |
|
|
f.write(file_body)
|
| 92 |
|
|
f.close()
|
| 93 |
|
|
|
| 94 |
|
|
def pack_container(self, config):
|
| 95 |
|
|
self.generate_container_lds([self.kernel_image_in, self.rootfs_elf_in, \
|
| 96 |
|
|
self.atags_elf_in])
|
| 97 |
|
|
self.generate_container_assembler([self.kernel_image_in, self.rootfs_elf_in, \
|
| 98 |
|
|
self.atags_elf_in])
|
| 99 |
|
|
os.system(config.toolchain_kernel + "gcc " + "-nostdlib -o %s -T%s %s" \
|
| 100 |
|
|
% (self.container_elf_out, self.container_lds_out, \
|
| 101 |
|
|
self.container_S_out))
|
| 102 |
|
|
# Final file is returned so that the final packer needn't
|
| 103 |
|
|
# get the packer object for this information
|
| 104 |
|
|
return self.container_elf_out
|
| 105 |
|
|
|
| 106 |
|
|
def clean(self):
|
| 107 |
|
|
if os.path.exists(self.container_elf_out):
|
| 108 |
|
|
shutil.rmtree(self.container_elf_out)
|
| 109 |
|
|
if os.path.exists(self.container_lds_out):
|
| 110 |
|
|
shutil.rmtree(self.container_lds_out)
|
| 111 |
|
|
if os.path.exists(self.container_S_out):
|
| 112 |
|
|
shutil.rmtree(self.container_S_out)
|
| 113 |
|
|
|
| 114 |
|
|
|
| 115 |
|
|
class DefaultContainerPacker:
|
| 116 |
|
|
def __init__(self, container, images_in):
|
| 117 |
|
|
|
| 118 |
|
|
# Here, we simply attempt to get PROJROOT/conts as
|
| 119 |
|
|
# PROJROOT/build/cont[0-9]
|
| 120 |
|
|
self.CONTAINER_BUILDDIR_BASE = \
|
| 121 |
|
|
source_to_builddir(join(PROJROOT,'conts'), container.id)
|
| 122 |
|
|
|
| 123 |
|
|
if not os.path.exists(self.CONTAINER_BUILDDIR_BASE):
|
| 124 |
|
|
os.mkdir(self.CONTAINER_BUILDDIR_BASE)
|
| 125 |
|
|
|
| 126 |
|
|
self.container_lds_out = join(self.CONTAINER_BUILDDIR_BASE, \
|
| 127 |
|
|
'container.lds')
|
| 128 |
|
|
self.container_S_out = join(self.CONTAINER_BUILDDIR_BASE, 'container.S')
|
| 129 |
|
|
self.container_elf_out = join(self.CONTAINER_BUILDDIR_BASE, \
|
| 130 |
|
|
'container' + str(container.id) + '.elf')
|
| 131 |
|
|
self.images_in = images_in
|
| 132 |
|
|
|
| 133 |
|
|
def generate_container_assembler(self, source):
|
| 134 |
|
|
with open(self.container_S_out, 'w+') as f:
|
| 135 |
|
|
file_body = ""
|
| 136 |
|
|
img_i = 0
|
| 137 |
|
|
for img in source:
|
| 138 |
|
|
file_body += container_assembler_body % (img_i, img)
|
| 139 |
|
|
img_i += 1
|
| 140 |
|
|
|
| 141 |
|
|
f.write(file_body)
|
| 142 |
|
|
f.close()
|
| 143 |
|
|
|
| 144 |
|
|
def generate_container_lds(self, source):
|
| 145 |
|
|
with open(self.container_lds_out, 'w+') as f:
|
| 146 |
|
|
img_i = 0
|
| 147 |
|
|
file_body = container_lds_start
|
| 148 |
|
|
for img in source:
|
| 149 |
|
|
file_body += container_lds_body % (img_i, img_i)
|
| 150 |
|
|
img_i += 1
|
| 151 |
|
|
file_body += container_lds_end
|
| 152 |
|
|
f.write(file_body)
|
| 153 |
|
|
f.close()
|
| 154 |
|
|
|
| 155 |
|
|
def pack_container(self, config):
|
| 156 |
|
|
self.generate_container_lds(self.images_in)
|
| 157 |
|
|
self.generate_container_assembler(self.images_in)
|
| 158 |
|
|
os.system(config.toolchain_kernel + "gcc " + "-nostdlib -o %s -T%s %s" \
|
| 159 |
|
|
% (self.container_elf_out, self.container_lds_out, \
|
| 160 |
|
|
self.container_S_out))
|
| 161 |
|
|
# Final file is returned so that the final packer needn't
|
| 162 |
|
|
# get the packer object for this information
|
| 163 |
|
|
return self.container_elf_out
|
| 164 |
|
|
|
| 165 |
|
|
def clean(self):
|
| 166 |
|
|
if os.path.exists(self.container_elf_out):
|
| 167 |
|
|
shutil.rmtree(self.container_elf_out)
|
| 168 |
|
|
if os.path.exists(self.container_lds_out):
|
| 169 |
|
|
shutil.rmtree(self.container_lds_out)
|
| 170 |
|
|
if os.path.exists(self.container_S_out):
|
| 171 |
|
|
shutil.rmtree(self.container_S_out)
|
| 172 |
|
|
|