summaryrefslogtreecommitdiff
path: root/update-uInitrd.sh
blob: 9abe2361cf59d7e2f18e26a6879bddcf35b07ad3 (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
#!/bin/bash

# Author: Linaro Android Team <linaro-dev@lists.linaro.org>
#
# These files are Copyright (C) 2012 Linaro Limited and they
# are licensed under the Apache License, Version 2.0.
# You may obtain a copy of this license at
# http://www.apache.org/licenses/LICENSE-2.0


function show_usage() {
    echo "Usage:"
    echo -e "\t`basename $0` [--serial|-s <serial>] [--partition|-p <boot-partition>] [--target|-t <target-file>] <source-file>"
    echo -e "\t\tUpdate the target file in uInitrd of the specified device"
    echo -e "\t\twith the specified source file"
    echo -e "\t\tWhen the target file is not specified, will update the file in"
    echo -e "\t\tthe root directory that has the same file name of the source file"
    echo ""
    echo -e "\t`basename $0` --help|-h"
    echo -e "\t\tPrint out this help information"
}

function parse_argv() {
    while test -n "$1"; do
        case "$1" in
            --help|-h)
                show_usage
                exit 0
                ;;
            --serial|-s)
                serial=$2;
                shift 2
                ;;
            --partition|-p)
                boot_partition=$2;
                shift 2
                ;;
            --target|-t)
                tgt_file=$2;
                shift 2
                ;;
            *)
                if [ -n "${src_file}" ]; then
                    show_usage
                    echo "Too many arguments, see --help for details"
                    exit 1
                else
                    src_file="$1"
                    shift
                fi
                ;;
        esac
    done
}

function export_serial(){
    if [ -n "${serial}" ]; then
        export ANDROID_SERIAL=${serial}
    else
        serial=`adb get-serialno|sed 's/\r//g'`
        if [ "X${serial}" == "Xunknown" ]; then
            echo "Can not get the serial number autotically,"
            echo "Please specify the serial number with the -s option"
            exit 1
        else
            export ANDROID_SERIAL=${serial}
        fi
    fi
}


function check_parameters(){
    if [ -z "${src_file}" ]; then
        echo "Please specify the path of the source file want to use"
        exit 1
    fi

    if [ ! -f "${src_file}" ]; then
        echo "The specified path(${src_file}) is not a file"
        exit 1
    fi

#    will use the basename of the src_file
#    if [ -z "${tgt_file}" ]; then
#        echo "Please specify the path of the target file want to be updated"
#        exit 1
#    fi
}

function check_device_status(){
    status=`adb get-state|sed 's/\r//g'`
    if [ "X${status}" != "Xdevice" ]; then
        echo "The device is not on line,"
        echo "Please check the device status"
        exit 1
    fi
}

function get_boot_partition(){
    product_device=`adb shell getprop ro.product.device|sed 's/\r//g'`
    case "X${product_device}" in
        "X" )
            echo "Failed to get the value of ro.build.product property"
            exit 1
            ;;
        "Xpandaboard" )
            boot_partition="mmcblk0p1"
            ;;
        "Xorigen" )
            boot_partition="mmcblk0p2"
            ;;
        "Xvexpress_a9" )
            boot_partition="mmcblk0p1"
            ;;
        "Xvexpress" )
            boot_partition="mmcblk0p1"
            ;;
        * )
            echo "The product device(${product_device}) is not supported!"
            exit 1
            ;;
    esac
}

function recreate_uinitrd(){
    uinitrd_f="$1"
    src_f="$2"
    tgt_f="$3"
    old_pwd=`pwd`

    src_dir=`dirname $src_f`
    src_name=`basename $src_f`
    cd $src_dir
    src_dir_abs_path=`pwd`
    src_file_abs="${src_dir_abs_path}/${src_name}"
    cd ${old_pwd}

    uinitrd_dir=`dirname $uinitrd_f`
    uinitrd_name=`basename $uinitrd_f`
    cd ${uinitrd_dir}

    dd if=${uinitrd_name} of=uInitrd.data ibs=64 skip=1
    mv uInitrd.data ramdisk.cpio.gz
    gzip -d -f ramdisk.cpio.gz
    cpio -i -F ramdisk.cpio
    cp -vf ${src_file_abs} ./${tgt_f}
    cpio -i -t -F ramdisk.cpio | cpio -o -H newc | gzip > ramdisk_new.cpio.gz
    mkimage -A arm -O linux -T ramdisk -n "Android Ramdisk Image" -d ramdisk_new.cpio.gz uInitrd

    cd ${old_pwd}
}

function update_uinitrd(){
    if [ -z "${boot_partition}" ]; then
        get_boot_partition
    fi
    adb shell mount -t vfat /dev/block/${boot_partition} ${mountpoint}

    org_uinitrd=`mktemp -d /tmp/uinitrd.XXX`
    echo "Pull the original uInitrd file for backup in ${org_uinitrd}"
    adb pull ${mountpoint}/uInitrd ${org_uinitrd} &>/dev/null
    cp -uvf $org_uinitrd/uInitrd{,.org}
    recreate_uinitrd "${org_uinitrd}/uInitrd" "${src_file}" "${tgt_file}"

    adb push ${org_uinitrd}/uInitrd ${mountpoint}/uInitrd
    adb push ${org_uinitrd}/uInitrd.org ${mountpoint}/uInitrd.org

    adb shell sync
    adb shell umount ${mountpoint}
    rm -fr ${org_uinitrd}
    echo "uInitrd updated successfully"
}

function main(){
    src_file=""
    tgt_file=""
    partition=""
    serial=""
    mountpoint="/data/local/tmp"

    parse_argv "$@"
    check_parameters
    if [ -z "$tgt_file" ]; then
        tgt_file=`basename ${src_file}`
    fi
    export_serial
    check_device_status
    update_uinitrd
}
main "$@"