OpenCores
URL https://opencores.org/ocsvn/c0or1k/c0or1k/trunk

Subversion Repositories c0or1k

[/] [c0or1k/] [trunk/] [scripts/] [linux/] [build_linux.py] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
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, string
9
from os.path import join
10
 
11
PROJRELROOT = '../../'
12
 
13
SCRIPTROOT = os.path.abspath(os.path.dirname("."))
14
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), PROJRELROOT)))
15
 
16
from config.projpaths import *
17
from config.configuration import *
18
from config.lib import *
19
 
20
LINUX_KERNEL_BUILDDIR = join(BUILDDIR, os.path.relpath(LINUX_KERNELDIR, PROJROOT))
21
 
22
# Create linux kernel build directory path as:
23
# conts/linux -> build/cont[0-9]/linux
24
def source_to_builddir(srcdir, id):
25
    cont_builddir = \
26
        os.path.relpath(srcdir, \
27
                        PROJROOT).replace("conts", \
28
                                          "cont" + str(id))
29
    return join(BUILDDIR, cont_builddir)
30
 
31
class LinuxUpdateKernel:
32
 
33
    def __init__(self, container):
34
        # List for setting/unsetting .config params of linux
35
        self.config_param_list = \
36
            (['PCI', 'SET'],['AEABI', 'SET'],
37
            ['SCSI', 'SET'],['BLK_DEV_SD', 'SET'],
38
            ['SYM53C8XX_2', 'SET'],['INPUT_EVDEV', 'SET'],
39
            ['INOTIFY', 'SET'],['DEBUG_INFO', 'SET'],
40
            ['USB_SUPPORT', 'UNSET'],['SOUND', 'UNSET'],)
41
 
42
        # List of CPUIDs, to be used by linux based on codezero config
43
        self.cpuid_list = (['ARM926', '0x41069265'],
44
                           ['CORTEXA8', '0x410fc080'],
45
                           ['ARM11MPCORE', '0x410fb022'],
46
                           ['CORTEXA9', '0x410fc090'])
47
        # List of ARCHIDs, to be used by linux based on codezero config
48
        self.archid_list = (['PB926', '0x183'],
49
                            ['EB', '0x33B'],
50
                            ['PB11MPCORE', '0x3D4'],
51
                            ['BEAGLE', '0x60A'],
52
                            ['PBA9', '0x76D'],
53
                            ['PBA8', '0x769'])
54
 
55
        # Path of system_macros header file
56
        self.system_macros_h_out = \
57
            join(LINUX_KERNELDIR,
58
                'arch/codezero/include/virtualization/system_macros.h')
59
        self.system_macros_h_in = \
60
            join(LINUX_KERNELDIR,
61
                'arch/codezero/include/virtualization/system_macros.h.in')
62
 
63
        #Path for kernel_param file
64
        self.kernel_param_out = \
65
            join(LINUX_KERNELDIR, 'arch/codezero/include/virtualization/kernel_param')
66
        self.kernel_param_in = \
67
                join(LINUX_KERNELDIR, 'arch/codezero/include/virtualization/kernel_param.in')
68
 
69
    # Replace line(having input_pattern) in filename with new_data
70
    def replace_line(self, filename, input_pattern, new_data, prev_line):
71
        with open(filename, 'r+') as f:
72
            flag = 0
73
            temp = 0
74
            x = re.compile(input_pattern)
75
            for line in f:
76
                if '' != prev_line:
77
                    if temp == prev_line and re.match(x, line):
78
                        flag = 1
79
                        break
80
                    temp = line
81
                else:
82
                    if re.match(x, line):
83
                        flag = 1
84
                        break
85
 
86
            if flag == 0:
87
                #print 'Warning: No match found for the parameter'
88
                return
89
            else:
90
                # Prevent recompilation in case kernel parameter is same
91
                if new_data != line:
92
                    f.seek(0)
93
                    l = f.read()
94
 
95
                    # Need to truncate file because, size of contents to be
96
                    # written may be less than the size of original file.
97
                    f.seek(0)
98
                    f.truncate(0)
99
 
100
                    # Write back to file
101
                    f.write(l.replace(line, new_data))
102
 
103
    # Update kernel parameters
104
    def update_kernel_params(self, config, container):
105
        # Update PAGE_OFFSET
106
        # FIXME: Find a way to add this in system_macros.h or kernel_param
107
        # issue is we have to update this in KCONFIG file which cannot
108
        # have dependency on other files.
109
        file = join(LINUX_KERNELDIR, 'arch/codezero/Kconfig')
110
        param = str(conv_hex(container.linux_page_offset))
111
        new_data = ('\t' + 'default ' + param + '\n')
112
        data_to_replace = "(\t)(default )"
113
        prev_line = ('\t'+'default 0x80000000 if VMSPLIT_2G' + '\n')
114
        self.replace_line(file, data_to_replace, new_data, prev_line)
115
 
116
    # Update ARCHID, CPUID and ATAGS ADDRESS
117
        for cpu_type, cpu_id in self.cpuid_list:
118
            if cpu_type == config.cpu.upper():
119
                cpuid = cpu_id
120
                break
121
        for arch_type, arch_id in self.archid_list:
122
            if arch_type == config.platform.upper():
123
                archid = arch_id
124
                break
125
 
126
        # Create system_macros header
127
        with open(self.system_macros_h_out, 'w+') as output:
128
            with open(self.system_macros_h_in, 'r') as input:
129
                output.write(input.read() % \
130
                    {'cpuid'        : cpuid, \
131
                     'archid'       : archid, \
132
                     'atags'        : str(conv_hex(container.linux_page_offset + 0x100)), \
133
                     'ztextaddr'    : str(conv_hex(container.linux_phys_offset)), \
134
                     'phys_offset'  : str(conv_hex(container.linux_phys_offset)), \
135
                     'page_offset'  : str(conv_hex(container.linux_page_offset)), \
136
                     'zreladdr'     : str(conv_hex(container.linux_zreladdr))})
137
 
138
        with open(self.kernel_param_out, 'w+') as output:
139
            with open(self.kernel_param_in, 'r') as input:
140
                output.write(input.read() % \
141
                      {'ztextaddr'    : str(conv_hex(container.linux_phys_offset)), \
142
                       'phys_offset'  : str(conv_hex(container.linux_phys_offset)), \
143
                       'page_offset'  : str(conv_hex(container.linux_page_offset)), \
144
                       'zreladdr'     : str(conv_hex(container.linux_zreladdr))})
145
 
146
    def modify_kernel_config(self, linux_builddir):
147
        file = join(linux_builddir, '.config')
148
        for param_name, param_value in self.config_param_list:
149
            param = 'CONFIG_' + param_name
150
            prev_line = ''
151
            if param_value == 'SET':
152
                data_to_replace = ('# ' + param)
153
                new_data = (param + '=y' + '\n')
154
            else:
155
                data_to_replace = param
156
                new_data = ('# ' + param + ' is not set' + '\n')
157
 
158
        self.replace_line(file, data_to_replace, new_data, prev_line)
159
 
160
class LinuxBuilder:
161
 
162
    def __init__(self, pathdict, container):
163
        self.LINUX_KERNELDIR = pathdict["LINUX_KERNELDIR"]
164
 
165
        # Calculate linux kernel build directory
166
        self.LINUX_KERNEL_BUILDDIR = \
167
            source_to_builddir(LINUX_KERNELDIR, container.id)
168
 
169
        self.container = container
170
        self.kernel_binary_image = \
171
            join(os.path.relpath(self.LINUX_KERNEL_BUILDDIR, LINUX_KERNELDIR), \
172
                 "vmlinux")
173
        self.kernel_image = join(self.LINUX_KERNEL_BUILDDIR, "linux.elf")
174
        self.kernel_updater = LinuxUpdateKernel(self.container)
175
 
176
        # Default configuration file to use based on selected platform
177
        self.platform_config_file = (['PB926', 'versatile_defconfig'],
178
                                     ['BEAGLE', 'omap3_beagle_defconfig'],
179
                                     ['PBA8', 'realview_defconfig'],
180
                                     ['PBA9', 'realview-smp_defconfig'],
181
                                     ['PB11MPCORE', 'realview-smp_defconfig'],)
182
 
183
    def build_linux(self, config):
184
        print '\nBuilding the linux kernel...'
185
        os.chdir(self.LINUX_KERNELDIR)
186
        if not os.path.exists(self.LINUX_KERNEL_BUILDDIR):
187
            os.makedirs(self.LINUX_KERNEL_BUILDDIR)
188
 
189
        for platform, config_file in self.platform_config_file:
190
            if platform == config.platform.upper():
191
                configuration_file = config_file
192
        os.system("make ARCH=codezero CROSS_COMPILE=" + \
193
                  config.toolchain_userspace + \
194
                  " O=" + self.LINUX_KERNEL_BUILDDIR + " " + configuration_file)
195
 
196
        self.kernel_updater.modify_kernel_config(self.LINUX_KERNEL_BUILDDIR)
197
        self.kernel_updater.update_kernel_params(config, self.container)
198
 
199
        os.system("make ARCH=codezero CROSS_COMPILE=" + \
200
                  config.toolchain_userspace + \
201
                  " O=" + self.LINUX_KERNEL_BUILDDIR + " menuconfig")
202
        os.system("make ARCH=codezero " + \
203
                  "CROSS_COMPILE=" + config.toolchain_userspace + \
204
                  " O=" + self.LINUX_KERNEL_BUILDDIR)
205
 
206
        # Generate kernel_image, elf to be used by codezero
207
        linux_elf_gen_cmd = (config.toolchain_userspace + "objcopy -R .note \
208
            -R .note.gnu.build-id -R .comment -S --change-addresses " + \
209
            str(conv_hex(-self.container.linux_page_offset + self.container.linux_phys_offset)) + \
210
            " " + self.kernel_binary_image + " " + self.kernel_image)
211
 
212
        #print cmd
213
        os.system(linux_elf_gen_cmd)
214
        print 'Done...'
215
 
216
    def clean(self):
217
        print 'Cleaning linux kernel build...'
218
        if os.path.exists(self.LINUX_KERNEL_BUILDDIR):
219
            shutil.rmtree(self.LINUX_KERNEL_BUILDDIR)
220
        print 'Done...'
221
 
222
if __name__ == "__main__":
223
    # This is only a default test case
224
    container = Container()
225
    container.id = 0
226
    linux_builder = LinuxBuilder(projpaths, container)
227
 
228
    if len(sys.argv) == 1:
229
        linux_builder.build_linux()
230
    elif "clean" == sys.argv[1]:
231
        linux_builder.clean()
232
    else:
233
        print " Usage: %s [clean]" % (sys.argv[0])

powered by: WebSVN 2.1.0

© copyright 1999-2025 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.