-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsingleimage.py
More file actions
285 lines (221 loc) · 10.1 KB
/
Copy pathsingleimage.py
File metadata and controls
285 lines (221 loc) · 10.1 KB
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#!/usr/bin/python
#===========================================================================
# This script creates a singleimage.bin
# REFERENCES
# $Header: //source/qcom/qct/core/pkg/bootloaders/rel/2.1.c10/boot_images/core/storage/tools/ptool/singleimage.py#1 $
# $DateTime: 2014/05/21 02:10:04 $
# $Author: coresvc $
# when who what, where, why
# -------- --- -------------------------------------------------------
# 2011-11-10 ah Will now also attempt to run checksparse.py if present
# 2011-09-07 ah Added logging
# 2011-08-09 ah Handle output directory -t, size in gb -g, more feedback
# 2011-07-26 ah first revision
# Copyright (c) 2007-2010
# Qualcomm Technologies Incorporated.
# All Rights Reserved.
# Qualcomm Confidential and Proprietary
# ===========================================================================*/
import struct, os, sys, getopt
import math
import re
import codecs
from types import *
import time
import subprocess as sub
from time import strftime, localtime
def reset_device_log():
try:
log_fp = open('log_singleimage.txt', 'w')
except:
print "\nERROR: Can't create the file log_singleimage.txt"
print "This might be because the file is open and locked"
print "Or because you are running this from a read-only location\n"
sys.exit()
print "\nCREATED log_singleimage.txt\n"
log_fp.close()
def device_log(message, display=1):
try:
log_fp = open('log_singleimage.txt', 'a')
except:
print "ERROR: could not open 'log_singleimage.txt'"
return
try:
log_fp.write("%s %s\n" % (strftime("%H:%M:%S", localtime()),message))
except:
print "ERROR: could not write to 'log_singleimage.txt'"
return
if display==1:
print message
log_fp.close()
##import pdb; pdb.set_trace()
reset_device_log()
device_log("\nsingleimage.py is running from CWD: %s\n" % os.getcwd())
def PrintBanner(sz):
device_log("\n"+"="*78)
device_log(sz)
device_log("="*78+"\n")
def find_file(filename, search_paths,BailIfFileNotFound=1):
if filename is None:
return None
device_log("\n\n\tLooking for '%s'"%filename)
device_log("\t"+"-"*40)
for x in search_paths:
device_log("\tSearching '%s'"%x)
temp = os.path.join(x, filename)
if os.path.exists(temp):
device_log("\n\t**Found %s (%i bytes)" % (temp,os.path.getsize(temp)))
return temp
## search cwd last
device_log("\tSearching '%s'"%os.getcwd())
if os.path.exists(filename):
device_log("\n\t**Found %s (%i bytes)" % (filename,os.path.getsize(filename)))
return filename
PrintBigError("\tCound't find file\n",BailIfFileNotFound)
return None
def PrintBigError(sz,Bail=1):
device_log("\t _________________ ___________ ")
device_log("\t| ___| ___ \\ ___ \\ _ | ___ \\")
device_log("\t| |__ | |_/ / |_/ / | | | |_/ /")
device_log("\t| __|| /| /| | | | / ")
device_log("\t| |___| |\\ \\| |\\ \\\\ \\_/ / |\\ \\ ")
device_log("\t\\____/\\_| \\_\\_| \\_|\\___/\\_| \\_|\n")
if len(sz)>0:
device_log(sz)
if Bail==1:
device_log("\nsingleimage.py exiting - Log is log_singleimage.txt\n")
sys.exit(1)
else:
device_log("\nNOT EXITING on this error, you can probably live without it\n")
def EnsureDirectoryExists(filename):
dir = os.path.dirname(filename)
try:
os.stat(dir)
except:
os.makedirs(dir)
def Usage():
device_log("\nUsage: Mass Storage Programmer - destructively writes data to disks!!\n")
sudo = ""
drive= ""
if sys.platform.startswith("linux"):
sudo = "sudo"
drive= "/dev/sdb"
else:
device_log("\tYou must run as Administrator under Win7")
device_log("\tYou must run as Administrator under Win7")
device_log("\tYou must run as Administrator under Win7\n")
drive="\\\\.\\PHYSICALDRIVE1"
device_log("%-40s\t python singleimage.py" % "Display this info")
device_log("%-40s\t%s python singleimage.py -x singleimage_partition_8960.xml" % ("Create normal singleimage.bin",sudo))
device_log("%-40s\t%s python singleimage.py -d 8192 -x singleimage_partition_8960.xml" % ("Create a 4MB singleimage.bin",sudo))
device_log("%-40s\t%s python singleimage.py -d 204800 -x singleimage_partition_8960.xml" % ("Create a 100MB singleimage.bin",sudo))
device_log("%-40s\t%s python singleimage.py -g 4 -x singleimage_partition_8960.xml" % ("Create a 4GB singleimage.bin",sudo))
device_log("\n%-40s\t%s python singleimage.py -s c:\windows -s d:\ -x singleimage_partition_8960.xml" % ("Search this path for files (-s)",sudo))
## ==============================================================================================
## ==============================================================================================
## ==============================================================================================
## =====main()===================================================================================
## ==============================================================================================
## ==============================================================================================
## ==============================================================================================
try:
opts, args = getopt.getopt(sys.argv[1:], "x:s:d:g:t:", ["xml=","search_path=","size_in_sectors=","size_in_GB=","location="])
except getopt.GetoptError, err:
# print help information and exit:
device_log(str(err)) # will print something like "option -a not recognized"
Usage()
device_log("\nsingleimage.py exiting - Log is log_singleimage.txt\n")
sys.exit(1)
search_paths = []
partition = None
size_in_sectors = 0
OutputFolder = None
for o, a in opts:
if o in ("-s", "--search_path"):
## also allow seperating commas
for x in a.strip("\n").split(","):
search_paths.append(x)
elif o in ("-x", "--xml"):
partition = a
elif o in ("-d", "--size_in_sectors"):
size_in_sectors = a
elif o in ("-g", "--size_in_GB"):
# i.e. 2GB = 2*1024*1024*1024
size_in_sectors = int(a)*1024*1024*1024/512
elif o in ("-t", "--location"):
OutputFolder = a
OutputFolder = re.sub(r"\\$","",OutputFolder) # remove final slash if it exists
OutputFolder = re.sub(r"/$","",OutputFolder) # remove final slash if it exists
OutputFolder += "/" # slashes will all be corrected below
if sys.platform.startswith("linux"):
OutputFolder = re.sub(r"\\","/",OutputFolder) # correct slashes
else:
OutputFolder = re.sub(r"/","\\\\",OutputFolder) # correct slashes
device_log("OutputFolder='%s'"%OutputFolder)
EnsureDirectoryExists(OutputFolder) # only need to call once
else:
assert False, "unhandled option"
if partition is None:
Usage()
device_log("\nsingleimage.py exiting - Log is log_singleimage.txt\n")
sys.exit(1)
device_log("\nSearch Paths")
device_log(search_paths)
## Step 1 - call ptool
device_log("\t _ _ ")
device_log("\t | | | | ")
device_log("\t _ __ | |_ ___ ___ | | _ __ _ _ ")
device_log("\t| '_ \\| __|/ _ \\ / _ \\| | | '_ \\| | | |")
device_log("\t| |_) | |_| (_) | (_) | |_| |_) | |_| |")
device_log("\t| .__/ \\__|\\___/ \\___/|_(_) .__/ \\__, |")
device_log("\t| | | | __/ |")
device_log("\t|_| |_| |___/ ")
ptool = find_file("ptool.py", search_paths)
partition = find_file(partition, search_paths)
if OutputFolder is not None:
sz = "python %s -g 1 -x %s -t %s" % (ptool,partition,OutputFolder)
else:
sz = "python %s -g 1 -x %s " % (ptool,partition)
PrintBanner(sz)
os.system(sz)
device_log("\n\n\t _ _ ")
device_log("\t | | | | ")
device_log("\t ___| |__ ___ ___| | _____ _ __ __ _ _ __ ___ ___ _ __ _ _ ")
device_log("\t / __| '_ \\ / _ \\/ __| |/ / __| '_ \\ / _` | '__/ __|/ _ \\ | '_ \\| | | |")
device_log("\t| (__| | | | __/ (__| <\\__ \\ |_) | (_| | | \\__ \\ __/_| |_) | |_| |")
device_log("\t \\___|_| |_|\\___|\\___|_|\\_\\___/ .__/ \\__,_|_| |___/\\___(_) .__/ \\__, |")
device_log("\t | | | | __/ |")
device_log("\t |_| |_| |___/ ")
checksparse = find_file("checksparse.py", search_paths,0)
rawprogram = find_file("rawprogram0.xml", search_paths)
if checksparse is None:
device_log("\nPlease NOTE: Without checksparse.py you cannot check for images that are sparse\n")
device_log("This might not be a big deal if your singleimage creation is only combining boot loaders\n")
device_log("If however you are trying to create a complete HLOS singleimage, you MIGHT BE IN TROUBLE!!! So be WARNED!!\n")
if checksparse is not None:
if OutputFolder is not None:
sz = "python %s -i %s -t %s " % (checksparse,rawprogram,OutputFolder)
else:
sz = "python %s -i %s " % (checksparse,rawprogram)
for path in search_paths:
sz += "-s %s " % path
PrintBanner(sz)
os.system(sz)
device_log("\n\n")
device_log("\t _ __ ___ ___ _ __ _ __ _ _ ")
device_log("\t| '_ ` _ \\/ __| '_ \\ | '_ \\| | | |")
device_log("\t| | | | | \\__ \\ |_) |_| |_) | |_| |")
device_log("\t|_| |_| |_|___/ .__/(_) .__/ \\__, |")
device_log("\t | | | | __/ |")
device_log("\t |_| |_| |___/ ")
msp = find_file("msp.py", search_paths)
patch = find_file("patch0.xml", search_paths)
if OutputFolder is not None:
sz = "python %s -r %s -p %s -d %d -t %s " % (msp,rawprogram,patch,size_in_sectors,OutputFolder)
else:
sz = "python %s -r %s -p %s -d %d " % (msp,rawprogram,patch,int(size_in_sectors))
for path in search_paths:
sz += "-s %s " % path
PrintBanner(sz)
os.system(sz)
device_log("\nsingleimage.py exiting - Log is log_singleimage.txt\n")