~aleteoryx/muditaos

muditaos/tools/macflash.sh -rw-r--r-- 1.9 KiB
a405cad6Aleteoryx trim readme 5 days ago
                                                                                
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
#!/bin/bash
# Copyright (c) 2017-2024, Mudita Sp. z.o.o. All rights reserved.
# For licensing, see https://github.com/mudita/MuditaOS/blob/master/LICENSE.md


IMAGE_FILE=""
OS_PARTITION_NAME="MUDITAOS"
PART_NODE=""
PHONE_DEV=""

function get_phone_dev() {
	BLOCK_DEV="/dev/r"$(diskutil info $OS_PARTITION_NAME | grep "Part of Whole" | awk '{print $4}')
	echo ${BLOCK_DEV}
}

function unmount_muditaos_partition() {
	PART_NODE=$(diskutil info $OS_PARTITION_NAME | grep "Device Node" | awk '{print $3}')
	diskutil unmount $PART_NODE  > /dev/null 2>&1
}

function eject_device() {
	diskutil eject $1
}

MAC_DD="gdd"

function test_if_run_as_root() {
    MY_NAME=$(whoami)
    if [[ "${MY_NAME}" == "root" ]]; then
        cat <<-MSGEND
			Please do not run this script as a root.
			Script will ask for your password for tasks it needs
			to run as a root (sudo ...)
			MSGEND
        exit 1
    fi
}

function test_if_gdd_installed() {
    if ! command -v $MAC_DD &> /dev/null; then
        echo "$MAC_DD command could not be found. Please, run macflash_setup.sh"
        exit 1
    fi
}

print_help() {
	echo "Usage: $0 -i image [-d device] [-h]"
	echo "    -i path to Pure/Bell image file"
	echo "    -d path do Pure/Bell block device (MSC)"
	echo "    -h print help"

	exit 1
}

if [ $# -lt 1 ]; then
    print_help
fi

while getopts "hi:d:" arg; do
	case "${arg}" in
		i)
			IMAGE_FILE=$OPTARG
			;;
		d)
			PHONE_DEV=$OPTARG
			;;
		h)
			print_help
			;;
		*)
			print_help
			;;
	esac
done

test_if_run_as_root
test_if_gdd_installed

if [ -z $PHONE_DEV ]; then
	PHONE_DEV=$(get_phone_dev)
fi

if [ ! -e $IMAGE_FILE ]; then
	echo "Image file $IMAGE_FILE does not exist"
	exit 1
fi

if [ ! -e $PHONE_DEV ]; then
	echo "Block device $PHONE_DEV does not exist"
	exit 1
fi

unmount_muditaos_partition

echo "Flashing $IMAGE_FILE to $PHONE_DEV..."

sudo gdd if=$IMAGE_FILE of=$PHONE_DEV bs=1M conv=sparse,fdatasync status=progress

eject_device $PHONE_DEV