summaryrefslogtreecommitdiff
path: root/tct/flash/flash.sh
blob: cdac1e34522c8bb7c8227deb1247c5036652aaaa (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
# Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Function uuid2dev() converts UUID stored in $FILE
# to corresponding /dev/sd*.
#
# Syntax is:
#   * uuid2dev $FILE [$SDMUX]
#   * change_uuid $FILE $DEV_SDCARD
#   * switch2device $SDMUX
#   * switch2testserver $SDMUX
#   * restart_device $SDMUX [$DELAY]
#   * sdmux_init $SDMUX
#   * change_id $SDMUX $NEWID
#   * dyper $SDMUX $DYPER $STATE
#   * dyper_restart $SDMUX $DYPER
#
# Author: Aleksander Mistewicz <a.mistewicz@samsung.com>

uuid2dev() {
    FILE="$1"
    SDMUX="$2"
    test -n "${FILE}" || die "Missing argument: file!"
    test -f "${FILE}" || die "${FILE} does not exist!"

    UUID="$(cat "${FILE}")"
    DEV="$(readlink "/dev/disk/by-uuid/${UUID}")"
    SLEEP=10

    echo "Searching: ${UUID}" >&2

    while [ -z "${DEV}" ] && [ "${SLEEP}" -le 30 ]
    do
        if [ -n "${SDMUX}" ] && [ "${SLEEP}" -ge 20 ]
        then
            echo "Restarting sdmux: ${SDMUX}" >&2
            switch2device "${SDMUX}"
            sdmux_init "${SDMUX}"
        fi
        sleep "${SLEEP}"
        SLEEP=$((SLEEP+10))
        DEV="$(readlink "/dev/disk/by-uuid/${UUID}")"
    done

    test -n "${DEV}" || die "DEV is empty, device not found!"

    DEV="${DEV##*/}"

    echo "/dev/${DEV%[0-9]}"
}

change_uuid() {
    FILE="$1"
    DEV_SDCARD="$2"
    test -n "${FILE}" || die "Missing argument: file!"
    test -n "${DEV_SDCARD}" || die "Missing argument: dev_sdcard!"

    NEW_UUID="$(uuidgen)"
    # 2nd partition is used because:
    #   * 1st partition is vfat (boot)
    #   * 3rd partition sometimes does not exists (i.e. wayland-efi)
    PART="${DEV_SDCARD}2"
    test -b "${PART}" || die "Missing device: required partition is no longer present!"

    # remove uninit_bg option from filesystem for UUID change. result is:
    #   * e2fsck is not required
    #   * UUID change is successful
    sudo tune2fs -O "^uninit_bg" "${PART}" ||
        die "Error returned by tune2fs: fs feature clear failed!" "$?"
    sudo tune2fs "${PART}" -U "${NEW_UUID}" ||
        die "Error returned by tune2fs: uuid change failed!" "$?"
    sudo tune2fs -O "+uninit_bg" "${PART}" ||
        echo "Error returned by tune2fs ($?): fs feature add failed!"

    echo "${NEW_UUID}" > "${FILE}"
}

switch2device() {
    SDMUX="$1"
    test -n "${SDMUX}" || die "Missing argument: sdmux!"
    # wait for an sdcard to finish all operations
    sleep 10
    sd-mux-ctrl -e "${SDMUX}" -d || die "Error returned by sd-mux-ctrl: switch2device failed!"
}

switch2testserver() {
    SDMUX="$1"
    test -n "${SDMUX}" || die "Missing argument: sdmux!"
    sd-mux-ctrl -e "${SDMUX}" -s ||
        die "Error returned by sd-mux-ctrl: switch2testserver failed!"
}

restart_device() {
    SDMUX="$1"
    DELAY="$2"
    test -n "${SDMUX}" || die "Missing argument: sdmux!"
    test -n "${DELAY}" || DELAY="2000"
    sd-mux-ctrl -e "${SDMUX}" -m "${DELAY}" -c ||
        die "Error returned by sd-mux-ctrl: restart_device failed!"
}

sdmux_init() {
    SDMUX="$1"
    test -n "${SDMUX}" || die "Missing argument: sdmux!"
    sd-mux-ctrl -e "${SDMUX}" -t ||
        die "Error returned by sd-mux-ctrl: sdmux_init failed!"
}

change_id() {
    SDMUX="$1"
    NEWID="$2"
    test -n "${SDMUX}" || die "Missing argument: sdmux!"
    test -n "${NEWID}" || die "Missing argument: newid!"
    if [ "${SDMUX}" != "${NEWID}" ]; then
        sd-mux-ctrl -e "${SDMUX}" -r "${NEWID}" ||
            die "Error returned by sd-mux-ctrl: change_id failed!"
    fi
}

dyper() {
    SDMUX="$1"
    DYPER="$2"
    STATE="$3"
    test -n "${SDMUX}" || die "Missing argument: sdmux!"
    test -n "${DYPER}" || die "Missing argument: dyper!"
    test -n "${STATE}" || die "Missing argument: state!"
    case "${STATE}" in
        on|off)
            sd-mux-ctrl -e "${SDMUX}" "--${DYPER}" "${STATE}" ||
                die "Error returned by sd-mux-ctrl: ${DYPER} failed!"
            ;;
        *)
            die "Unknown state: ${STATE}"
            ;;
    esac
}

dyper_restart() {
    SDMUX="$1"
    DYPER="$2"
    test -n "${SDMUX}" || die "Missing argument: sdmux!"
    test -n "${DYPER}" || die "Missing argument: dyper!"
    dyper "${SDMUX}" "${DYPER}" on
    sleep 1
    dyper "${SDMUX}" "${DYPER}" off
}