Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/image-writer
blob: 99f57b3be42959b5814b457302a01542ef66cf34 (plain)
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
#!/usr/bin/python -ttu
# vim: ai ts=4 sts=4 et sw=4

#    Copyright (c) 2008 Intel Corporation
#
#    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; version 2 of the License
#
#    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, write to the Free Software Foundation, Inc., 59
#    Temple Place - Suite 330, Boston, MA 02111-1307, USA.

import sys
import os
import gettext
import re
import subprocess
import time
import select

_ = gettext.lgettext
PROG_NAME = "image-writer"
COLOR_BLACK = "\033[00m"
COLOR_RED =   "\033[1;31m"
COLOR_BLUE =  "\033[1;34m"

def main():

    # Python version check
    if sys.hexversion < 0x2040000:
        print >> sys.stderr, _("Error: %s depends on a Python " \
                               "v2.4 or greater!") % (PROG_NAME)
        sys.exit(1)

    # Check for root priveleges
    if not are_we_root():
        print >> sys.stderr, _("%s must be run using " \
                               "root priveleges") % (PROG_NAME)
        sys.exit(1)

    # Parameters check
    if len(sys.argv) != 2:
        display_help()
        sys.exit(1)

    # Check image validity
    image = os.path.realpath(os.path.abspath(sys.argv[1]))
    if not is_valid_image(image):
        sys.exit(1)

    # Get USB drive
    usbd=get_usb_disk()
    if len(usbd)==0:
        sys.exit(1)

    # Unmount USB drive
    if not umount_device(usbd):
        sys.exit(1)

    # Warn of pending USB content doom
    msg = _("Warning:  The USB drive (%s) will be completely erased!") % usbd
    if not user_confirm (msg, COLOR_RED):
        sys.exit(1)

    # Write image to USB drive
    if not write_image_to_disk (image, usbd):
        sys.exit(1)

    print _("You may now boot your mobile device with this USB drive\n")
    return 0

#-----------------------------------------------------------------------

def display_help():
    print _("\nUsage: image-writer <full-path-to-image>\n")

# ret True = running as root
def are_we_root():
    if os.getuid() == 0:
        return True
    return False

def is_valid_image(image):
    if not os.path.isfile(image):
        print _("The image path does not point to a file")
        return False;
    if not (os.path.getsize(image) > (180*1024*1024)):
        msg = _("The file specified does not appear to be " \
                "large enough to be a valid image.")
        if not user_confirm (msg):
            return False;
    return True

def user_confirm(msg, text_color=COLOR_BLACK,
                 confirm=_(" Do you want to continue anyway? (y/n) ")):
    print _("%s%s%s") % (text_color, msg, COLOR_BLACK)
    confirm_msg = _("%s%s%s") % (text_color, confirm, COLOR_BLACK)
    name=""
    while name!="n" and name!="y":
        name = raw_input(confirm_msg)
        name = name.lower()
    if name=="n":
        return False;
    return True

def get_usb_disk():
    usbd = ""
    udisks = get_current_udisks();
    if len(udisks)==0:
        print _("No USB drives detected.")
        print _("Please insert a USB drive large enough to store the " \
                "given image")
        return ""
    elif len(udisks)==1:
        usbd=udisks[0]
    elif len(udisks)>0:
        print _("\nMultiple USB drives discovered:")
        i=1
        for usbd in udisks:
            print "%s) %s" % (i, usbd)
            i+=1
        sel_usb = ""
        while sel_usb not in range (1,len(udisks)+1):
            try:
                sel_usb = int(raw_input(" Select the USB " \
                                        "drive to use (1-%s): " % (len(udisks))))
            except ValueError:
                continue
        usbd = udisks[sel_usb-1]
    #print "Drive selected: '%s'" % (usbd)
    return usbd

def get_current_udisks():
    usb_devices = []
    dirname = os.path.realpath(os.path.abspath('/sys/bus/scsi'))
    work_list = get_usb_dir_tree(dirname)
    usb_list = [ x for x in work_list if re.search(r'usb', x) ]
    for filename in usb_list:
        #print _("usb_list file is %s") % filename
        device_dir = os.path.join('/sys/devices', filename)
        block_dir = os.path.join(device_dir, 'block')
        if (os.path.isdir(block_dir)) :
            for result in os.listdir(block_dir):
                #print result
                usb_dev = os.path.join('/dev',result)
                if os.path.exists(usb_dev):
                    usb_devices.append(usb_dev)
                break
        elif os.path.isdir(device_dir):
            for device_file in os.listdir(device_dir):
                full_path = os.path.join(device_dir, device_file)
                result = re.search(r'^block:(?P<dev>.*)', device_file)
                if result:
                    usb_dev = os.path.join('/dev', result.group('dev'))
                    if os.path.exists(usb_dev):
                        usb_devices.append(usb_dev)
                    break
    return usb_devices

def get_usb_dir_tree(dirname):
    file_set = set()
    for filename in os.listdir(dirname):
        full_path = os.path.join(dirname, filename)
        if os.path.islink(full_path):
            file_set.add(os.path.realpath(full_path))
        elif os.path.isdir(full_path):
            file_set.update(get_usb_dir_tree(full_path))
        else:
            file_set.add(full_path)
    return file_set

def umount_device(device):
    """umount a device if it is mounted"""
    dev_path = "%s1 " % os.path.realpath(os.path.abspath(device))

    if is_mounted(dev_path):
        print _("Unmounting %s...") % (dev_path) , 
        result = os.system("umount %s" % (dev_path))
        if result and is_mounted(dev_path):
            print _("Failed.\n%s could not be unmounted") % device
            return False
        print _("Done.")
    return True

def is_mounted (dirname):
    mount_file = open('/proc/mounts', 'r')
    for line in mount_file:
        if line.find(dirname) == 0:
            #return True  (causes failures later)
            return False
    return False

def write_image_to_disk (image_filename, usb_disk):
# image write timings
#   200 == 78s
#   422 == 165s
#  1036 == 413s
    size = os.path.getsize(image_filename)
    print _("Source:      %s") % image_filename
    print _("Size:        %s MB") % (int(size/(1024*1024)))
    print _("Destination: %s") % usb_disk
    #(tbd, get usb capacity): print _("Capacity:    %s") % ("1GB")

    # get time estimate based on image size (assume 10MB/sec)
    totsec = int(size / (5*1024*1024))
    min,sec = divmod(totsec, 60)

    msg = _("Writing image (Est. %smin %ssec)...    ") % (min,sec)
    print _("%s%s%s") % (COLOR_BLUE, msg, COLOR_BLACK) ,

    cmd = "dd bs=4096 if=%s of=%s" % (image_filename, usb_disk)

    p = subprocess.Popen(cmd.split(), 
                         stdout = subprocess.PIPE, 
                         stderr = subprocess.STDOUT, 
                         stdin = subprocess.PIPE, 
                         close_fds = True)

    # show progress (percentage ticking)
    poll = select.poll()
    poll.register(p.stdout, select.POLLIN)
    interval = (totsec / 100.0)
    perc = 0
    while p.poll() == None:
        print _("%s\b\b\b\b%2s%%%s") % (COLOR_BLUE, perc, COLOR_BLACK) ,
        time.sleep(interval)
        if perc < 99:
            perc+=1
    print _("%s\b\b\b\b100%%%s") % (COLOR_BLUE, COLOR_BLACK)

    # show output of command
    (sout,serr) = p.communicate()
    for line in sout.split('\n'):
        print line

    result = p.returncode
    if result != 0:
        print _("Error:  The image could not be written to the USB drive")
        return False
    print _("The image was successfully written to the USB drive")
    return True


if __name__ == '__main__':
    sys.exit(main())