~salgado/linaro-image-tools/bug-702645

« back to all changes in this revision

Viewing changes to linaro_media_create/populate_boot.py

Move remaining board-specific code from populate_boot.py to boards.py

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
import errno
2
 
import glob
3
2
import os
4
3
 
5
4
from linaro_media_create import cmd_runner
6
5
 
7
6
 
8
 
def _run_mkimage(img_type, load_addr, entry_point, name, img_data, img,
9
 
                 stdout=None, as_root=True):
10
 
    cmd = ['mkimage',
11
 
           '-A', 'arm',
12
 
           '-O', 'linux',
13
 
           '-T', img_type,
14
 
           '-C', 'none',
15
 
           '-a', load_addr,
16
 
           '-e', load_addr,
17
 
           '-n', name,
18
 
           '-d', img_data,
19
 
           img]
20
 
    proc = cmd_runner.run(cmd, as_root=as_root, stdout=stdout)
21
 
    proc.wait()
22
 
    return proc.returncode
23
 
 
24
 
 
25
 
def _get_file_matching(regex):
26
 
    """Return a file whose path matches the given regex.
27
 
 
28
 
    If zero or more than one files match, raise a ValueError.
29
 
    """
30
 
    files = glob.glob(regex)
31
 
    if len(files) == 1:
32
 
        return files[0]
33
 
    elif len(files) == 0:
34
 
        raise ValueError(
35
 
            "No files found matching '%s'; can't continue" % regex)
36
 
    else:
37
 
        # TODO: Could ask the user to chosse which file to use instead of
38
 
        # raising an exception.
39
 
        raise ValueError("Too many files matching '%s' found." % regex)
40
 
 
41
 
 
42
 
def make_uImage(load_addr, uboot_parts_dir, sub_arch, boot_disk):
43
 
    img_data = _get_file_matching(
44
 
        '%s/vmlinuz-*-%s' % (uboot_parts_dir, sub_arch))
45
 
    img = '%s/uImage' % boot_disk
46
 
    return _run_mkimage(
47
 
        'kernel', load_addr, load_addr, 'Linux', img_data, img)
48
 
 
49
 
 
50
 
def make_uInitrd(uboot_parts_dir, sub_arch, boot_disk):
51
 
    img_data = _get_file_matching(
52
 
        '%s/initrd.img-*-%s' % (uboot_parts_dir, sub_arch))
53
 
    img = '%s/uInitrd' % boot_disk
54
 
    return _run_mkimage('ramdisk', '0', '0', 'initramfs', img_data, img)
55
 
 
56
 
 
57
 
def make_boot_script(boot_script_data, tmp_dir, boot_script):
58
 
    # Need to save the boot script data into a file that will be passed to
59
 
    # mkimage.
60
 
    data_file = '%s/boot.cmd' % tmp_dir
61
 
    with open(data_file, 'w') as fd:
62
 
        fd.write(boot_script_data)
63
 
    return _run_mkimage(
64
 
        'script', '0', '0', 'boot script', data_file, boot_script)
65
 
 
66
 
 
67
 
def install_mx51evk_boot_loader(imx_file, boot_device_or_file):
68
 
    proc = cmd_runner.run([
69
 
        "dd",
70
 
        "if=%s" % imx_file,
71
 
        "of=%s" % boot_device_or_file,
72
 
        "bs=1024",
73
 
        "seek=1",
74
 
        "conv=notrunc"], as_root=True)
75
 
    proc.wait()
76
 
 
77
 
 
78
 
def install_omap_boot_loader(mlo_file, boot_disk):
79
 
    cmd_runner.run(["cp", "-v", mlo_file, boot_disk], as_root=True).wait()
80
 
    # XXX: Is this really needed?
81
 
    cmd_runner.run(["sync"]).wait()
82
 
 
83
 
 
84
 
def make_boot_ini(boot_script, boot_disk):
85
 
    proc = cmd_runner.run(
86
 
        ["cp", "-v", boot_script, "%s/boot.ini" % boot_disk], as_root=True)
87
 
    proc.wait()
88
 
 
89
 
 
90
 
def populate_boot(board, board_config, chroot_dir, boot_partition, boot_disk,
91
 
                  boot_device_or_file, tmp_dir, is_live, is_lowmem, consoles):
 
7
def populate_boot(board_config, chroot_dir, boot_partition, boot_disk,
 
8
                  boot_device_or_file, is_live, is_lowmem, consoles):
92
9
 
93
10
    parts_dir = 'boot'
94
11
    if is_live:
115
32
        dict(boot_disk=boot_disk,
116
33
             boot_script_name=board_config.boot_script))
117
34
 
118
 
    load_addr = board_config.load_addr
119
 
    sub_arch = board_config.sub_arch
120
 
    boot_cmd = board_config.get_boot_cmd(is_live, is_lowmem, consoles)
121
 
 
122
 
    # TODO: Once linaro-media-create is fully ported to python, we should
123
 
    # split this into several board-specific functions that are defined
124
 
    # somewhere else and just called here.
125
 
    if board in ["beagle", "panda"]:
126
 
        xloader_dir = 'x-loader-omap'
127
 
        if board == "panda":
128
 
            xloader_dir = 'x-loader-omap4'
129
 
        mlo_file = os.path.join(
130
 
            chroot_dir, 'usr', 'lib', xloader_dir, 'MLO')
131
 
        install_omap_boot_loader(mlo_file, boot_disk)
132
 
        make_uImage(load_addr, uboot_parts_dir, sub_arch, boot_disk)
133
 
        make_uInitrd(uboot_parts_dir, sub_arch, boot_disk)
134
 
        make_boot_script(boot_cmd, tmp_dir, boot_script)
135
 
        make_boot_ini(boot_script, boot_disk)
136
 
 
137
 
    elif board == "igep":
138
 
        make_uImage(load_addr, uboot_parts_dir, sub_arch, boot_disk)
139
 
        make_uInitrd(uboot_parts_dir, sub_arch, boot_disk)
140
 
        make_boot_script(boot_cmd, tmp_dir, boot_script)
141
 
        make_boot_ini(boot_script, boot_disk)
142
 
 
143
 
    elif board == "ux500":
144
 
        make_uImage(load_addr, uboot_parts_dir, sub_arch, boot_disk)
145
 
        make_uInitrd(uboot_parts_dir, sub_arch, boot_disk)
146
 
        make_boot_script(boot_cmd, tmp_dir, boot_script)
147
 
 
148
 
    elif board == "vexpress":
149
 
        make_uImage(load_addr, uboot_parts_dir, sub_arch, boot_disk)
150
 
        make_uInitrd(uboot_parts_dir, sub_arch, boot_disk)
151
 
 
152
 
    elif board == "mx51evk":
153
 
        install_mx51evk_boot_loader(
154
 
            "binary/usr/lib/u-boot/mx51evk/u-boot.imx", boot_device_or_file)
155
 
        make_uImage(load_addr, uboot_parts_dir, sub_arch, boot_disk)
156
 
        make_uInitrd(uboot_parts_dir, sub_arch, boot_disk)
157
 
        make_boot_script(boot_cmd, tmp_dir, boot_script)
158
 
 
159
 
    else:
160
 
        raise AssertionError(
161
 
            "Internal error; missing support for board: %s" % board)
 
35
    board_config.make_boot_files(
 
36
        uboot_parts_dir, is_live, is_lowmem, consoles, chroot_dir, boot_disk,
 
37
        boot_script, boot_device_or_file)
162
38
 
163
39
    cmd_runner.run(['sync']).wait()
164
40
    try: