OLD | NEW |
1 #!/bin/bash | 1 #!/bin/bash |
2 | 2 |
3 # Copyright (c) 2010 The Chromium OS Authors. All rights reserved. | 3 # Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
4 # Use of this source code is governed by a BSD-style license that can be | 4 # Use of this source code is governed by a BSD-style license that can be |
5 # found in the LICENSE file. | 5 # found in the LICENSE file. |
6 | 6 |
7 # Determine script directory | 7 # Determine script directory |
8 SCRIPT_DIR=$(dirname $0) | 8 SCRIPT_DIR=$(dirname $0) |
9 PROG=$(basename $0) | 9 PROG=$(basename $0) |
10 GPT=cgpt | 10 GPT=cgpt |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
78 sudo mount -o loop,ro,offset=$((offset * 512)) "$image" "$mount_dir" | 78 sudo mount -o loop,ro,offset=$((offset * 512)) "$image" "$mount_dir" |
79 } | 79 } |
80 | 80 |
81 # Mount a partition from an image into a local directory | 81 # Mount a partition from an image into a local directory |
82 # Args: IMAGE PARTNUM MOUNTDIRECTORY | 82 # Args: IMAGE PARTNUM MOUNTDIRECTORY |
83 mount_image_partition() { | 83 mount_image_partition() { |
84 local image=$1 | 84 local image=$1 |
85 local partnum=$2 | 85 local partnum=$2 |
86 local mount_dir=$3 | 86 local mount_dir=$3 |
87 local offset=$(partoffset "$image" "$partnum") | 87 local offset=$(partoffset "$image" "$partnum") |
| 88 # Forcibly call enable_rw_mount. It should fail on unsupported filesystems |
| 89 # and be idempotent on ext*. |
| 90 enable_rw_mount "$image" $((offset * 512)) 2> /dev/null |
88 sudo mount -o loop,offset=$((offset * 512)) "$image" "$mount_dir" | 91 sudo mount -o loop,offset=$((offset * 512)) "$image" "$mount_dir" |
89 if is_rootfs_partition "$mount_dir"; then | 92 if is_rootfs_partition "$mount_dir"; then |
90 tag_as_needs_to_be_resigned "$mount_dir" | 93 tag_as_needs_to_be_resigned "$mount_dir" |
91 fi | 94 fi |
92 } | 95 } |
93 | 96 |
94 # Extract a partition to a file | 97 # Extract a partition to a file |
95 # Args: IMAGE PARTNUM OUTPUTFILE | 98 # Args: IMAGE PARTNUM OUTPUTFILE |
96 extract_image_partition() { | 99 extract_image_partition() { |
97 local image=$1 | 100 local image=$1 |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
154 for filename in "$@"; do | 157 for filename in "$@"; do |
155 if [ ! -f "$filename" -a ! -b "$filename" ]; then | 158 if [ ! -f "$filename" -a ! -b "$filename" ]; then |
156 echo "ERROR: Cannot find required file: $filename" | 159 echo "ERROR: Cannot find required file: $filename" |
157 return_value=1 | 160 return_value=1 |
158 fi | 161 fi |
159 done | 162 done |
160 | 163 |
161 return $return_value | 164 return $return_value |
162 } | 165 } |
163 | 166 |
| 167 # For details, see crosutils.git/common.sh |
| 168 enable_rw_mount() { |
| 169 local rootfs="$1" |
| 170 local offset="${2-0}" |
| 171 |
| 172 # Make sure we're checking an ext2 image |
| 173 if ! is_ext2 "$rootfs" $offset; then |
| 174 echo "enable_rw_mount called on non-ext2 filesystem: $rootfs $offset" 1>&2 |
| 175 return 1 |
| 176 fi |
| 177 |
| 178 local ro_compat_offset=$((0x464 + 3)) # Set 'highest' byte |
| 179 # Dash can't do echo -ne, but it can do printf "\NNN" |
| 180 # We could use /dev/zero here, but this matches what would be |
| 181 # needed for disable_rw_mount (printf '\377'). |
| 182 printf '\000' | |
| 183 sudo dd of="$rootfs" seek=$((offset + ro_compat_offset)) \ |
| 184 conv=notrunc count=1 bs=1 |
| 185 } |
| 186 |
| 187 # For details, see crosutils.git/common.sh |
| 188 is_ext2() { |
| 189 local rootfs="$1" |
| 190 local offset="${2-0}" |
| 191 |
| 192 # Make sure we're checking an ext2 image |
| 193 local sb_magic_offset=$((0x438)) |
| 194 local sb_value=$(sudo dd if="$rootfs" skip=$((offset + sb_magic_offset)) \ |
| 195 count=2 bs=1 2>/dev/null) |
| 196 local expected_sb_value=$(printf '\123\357') |
| 197 if [ "$sb_value" = "$expected_sb_value" ]; then |
| 198 return 0 |
| 199 fi |
| 200 return 1 |
| 201 } |
| 202 |
| 203 disable_rw_mount() { |
| 204 local rootfs="$1" |
| 205 local offset="${2-0}" |
| 206 |
| 207 # Make sure we're checking an ext2 image |
| 208 if ! is_ext2 "$rootfs" $offset; then |
| 209 echo "disable_rw_mount called on non-ext2 filesystem: $rootfs $offset" 1>&2 |
| 210 return 1 |
| 211 fi |
| 212 |
| 213 local ro_compat_offset=$((0x464 + 3)) # Set 'highest' byte |
| 214 # Dash can't do echo -ne, but it can do printf "\NNN" |
| 215 # We could use /dev/zero here, but this matches what would be |
| 216 # needed for disable_rw_mount (printf '\377'). |
| 217 printf '\377' | |
| 218 sudo dd of="$rootfs" seek=$((offset + ro_compat_offset)) \ |
| 219 conv=notrunc count=1 bs=1 |
| 220 } |
| 221 |
| 222 rw_mount_disabled() { |
| 223 local rootfs="$1" |
| 224 local offset="${2-0}" |
| 225 |
| 226 # Make sure we're checking an ext2 image |
| 227 if ! is_ext2 "$rootfs" $offset; then |
| 228 return 2 |
| 229 fi |
| 230 |
| 231 local ro_compat_offset=$((0x464 + 3)) # Set 'highest' byte |
| 232 local ro_value=$(sudo dd if="$rootfs" skip=$((offset + ro_compat_offset)) \ |
| 233 count=1 bs=1 2>/dev/null) |
| 234 local expected_ro_value=$(printf '\377') |
| 235 if [ "$ro_value" = "$expected_ro_value" ]; then |
| 236 return 0 |
| 237 fi |
| 238 return 1 |
| 239 } |
| 240 |
164 trap "cleanup_temps_and_mounts" EXIT | 241 trap "cleanup_temps_and_mounts" EXIT |
165 | 242 |
OLD | NEW |