-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwrite_bootable_mmc.py
executable file
·220 lines (181 loc) · 6.18 KB
/
write_bootable_mmc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#!/usr/bin/env python
'''
# Copyright (C) 2020, Elphel.inc.
# Usage: known
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http:#www.gnu.org/licenses/>.
@author: Oleg K Dzhimiev
@copyright: 2016 Elphel, Inc.
@license: GPLv3.0+
@contact: oleg@elphel.com
@deffield updated: unknown
'''
__author__ = "Elphel"
__copyright__ = "Copyright 2020, Elphel, Inc."
__license__ = "GPL"
__version__ = "3.0+"
__maintainer__ = "Oleg K Dzhimiev"
__email__ = "oleg@elphel.com"
__status__ = "Development"
import subprocess
import sys
import os
import time
# functions
# useful link 1: http://superuser.com/questions/868117/layouting-a-disk-image-and-copying-files-into-it
# useful link 2: poky/scripts/contrib/mkefidisk.sh
# (?) useful link 3: http://unix.stackexchange.com/questions/53890/partitioning-disk-image-file
def shout(cmd):
#subprocess.call prints to console
subprocess.call(cmd,shell=True)
def print_help():
print("\nDesctiption:\n")
print(" * Required programs: kpartx")
print(" * Run under superuser. Make sure the correct device is provided.")
print(" * Erases partition table on the provided device")
print(" * If given someimage.img file - burns the sd card from it")
print(" * If not - uses the files from the predefined list")
print(" * Creates FAT32 partition labeled 'BOOT' and copies files required for boot")
print(" * Creates EXT4 partition labeled 'root' and extracts rootfs.tar.gz")
print("\nExamples:\n")
print(" * Use files (names are hardcoded) from the current dir ('build/tmp/deploy/images/elphel393/mmc/'):")
print(" ~$ python make_sdcard.py /dev/sdz")
print(" * Use someimage.img file:")
print(" ~$ python make_sdcard.py /dev/sdz someimage.img")
print(" * To write *.iso use a standard os tool that burns bootable USB drives")
print("")
def check_program_installed(program):
try:
result = subprocess.check_output("which "+program,shell=True)
except subprocess.CalledProcessError:
print("Program missing: "+program)
return False
return True
required_programs = (
"parted",
"kpartx"
)
something_is_missing = False
for i in required_programs:
tmpres = check_program_installed(i)
something_is_missing = something_is_missing or (not tmpres)
if something_is_missing:
sys.exit()
if len(sys.argv) > 1:
DEVICE = sys.argv[1]
else:
DEVICE = ""
print_help()
sys.exit()
if len(sys.argv) > 2:
IMAGE_FILE = sys.argv[2]
if not IMAGE_FILE.endswith(".img"):
print("ERROR: Please, provide *.img file or leave argument empty to use certain image files in the current dir")
sys.exit()
else:
IMAGE_FILE = ""
print("NOTE: if plasma crash do not worry")
#params
SDCARD_SIZE = 4000
PT_TYPE = "msdos"
BOOT_LABEL = "BOOT"
BOOT_FS = "fat32"
BOOT_SIZE = 128
BOOT_FILE_LIST = (
"boot.bin",
"u-boot-dtb.img",
"devicetree.dtb",
"uImage"
)
ROOT_LABEL = "root"
ROOT_FS = "ext4"
ROOT_ARCHIVE = "rootfs.tar.gz"
something_is_missing = False
if IMAGE_FILE=="":
print("Preparing SD card using files: "+str(BOOT_FILE_LIST + (ROOT_ARCHIVE,)))
for f in BOOT_FILE_LIST + (ROOT_ARCHIVE,):
if not os.path.isfile(f):
print("file "+f+" is missing")
something_is_missing = True
else:
print("Preparing SD card from "+IMAGE_FILE)
if not os.path.isfile(IMAGE_FILE):
print("No such file")
something_is_missing = True
if not os.path.exists(DEVICE):
print("No such device")
something_is_missing = True
if something_is_missing:
sys.exit()
print("= Erase partition table on "+DEVICE+" and everything else")
#shout("dd if=/dev/zero of="+DEVICE+" bs=1M count="+str(BOOT_SIZE+2))
shout("dd if=/dev/zero of="+DEVICE+" bs=512 count=2")
shout("dd if=/dev/zero of="+DEVICE+" bs=1MB count=1 seek=1")
shout("dd if=/dev/zero of="+DEVICE+" bs=1MB count=1 seek="+str(BOOT_SIZE))
print("= Create partition table")
shout("parted -s "+DEVICE+" mktable "+PT_TYPE)
print("= Create FAT partition")
shout("parted -s "+DEVICE+" mkpart primary "+BOOT_FS+" 1 "+str(BOOT_SIZE))
shout("parted -s "+DEVICE+" mkpart primary "+ROOT_FS+" "+str(BOOT_SIZE+1)+" 100%")
# no need?
shout("parted -s "+DEVICE+" align-check optimal 1")
shout("parted -s "+DEVICE+" align-check optimal 2")
#sys.exit()
devs_created = False
while not devs_created:
if (os.path.exists(DEVICE+"1")) and (os.path.exists(DEVICE+"2")):
devs_created = True
else:
print("waiting")
time.sleep(0.5)
time.sleep(1)
print("= Format")
shout("mkfs.vfat "+DEVICE+"1 -F 32 -n "+BOOT_LABEL)
shout("mkfs.ext4 "+DEVICE+"2 -F -L "+ROOT_LABEL)
shout("mkdir tmp")
if IMAGE_FILE=="":
shout("mount "+DEVICE+"1 tmp")
for i in BOOT_FILE_LIST:
print(" "+i)
shout("cp "+i+" tmp")
shout("umount tmp")
shout("mount "+DEVICE+"2 tmp")
shout("tar -C tmp/ -xzpf "+ROOT_ARCHIVE)
shout("umount tmp")
else:
shout("modprobe dm-mod")
shout("kpartx -av "+IMAGE_FILE)
#wait for devices
devs_created = False
while not devs_created:
if (os.path.exists("/dev/mapper/loop0p1")) and (os.path.exists("/dev/mapper/loop0p2")):
devs_created = True
else:
print("waiting")
time.sleep(0.5)
shout("mkdir tmp2")
shout("mount "+DEVICE+"1 tmp")
shout("mount /dev/mapper/loop0p1 tmp2")
shout("rsync -a tmp2/ tmp")
shout("umount tmp")
shout("umount tmp2")
shout("mount "+DEVICE+"2 tmp")
shout("mount /dev/mapper/loop0p2 tmp2")
shout("rsync -a tmp2/ tmp")
shout("umount tmp")
shout("umount tmp2")
shout("rm -rf tmp2")
shout("kpartx -dv "+IMAGE_FILE)
shout("rm -rf tmp")
print("Done")