-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathupdate_attempter.sh
executable file
·392 lines (338 loc) · 11.2 KB
/
update_attempter.sh
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
#!/bin/bash
# 2020 (c) Muntashir Al-Islam. All rights reserved.
# Source: https://chromium.googlesource.com/aosp/platform/system/update_engine/+/a1f4a7dcaa921fcb0ab395214a9558a62ca083f2/update_attempter.cc
# Fetched 2 Jan 2020
# Dependencies: RealSystemState, CertificateChecker, BootControlInterface, UpdateManager
# Get script directory
SCRIPT_DIR="$(dirname "$(readlink -f "$0")")"
[ command -v debug >/dev/null 2>&1 ] || source "${SCRIPT_DIR}/debug_utils.sh"
# Don't ever break the sequence!
. "${SCRIPT_DIR}/update_status.py"
. "${SCRIPT_DIR}/image_properties.sh"
. "${SCRIPT_DIR}/omaha_request_params.sh"
. "${SCRIPT_DIR}/power_manager.sh"
. "${SCRIPT_DIR}/boot_control_chromeos.sh"
. "${SCRIPT_DIR}/common/prefs.sh"
. "${SCRIPT_DIR}/common/constants.sh"
. "${SCRIPT_DIR}/common/platform_constants.sh"
. "${SCRIPT_DIR}/update_status_utils.sh"
kMaxDeltaUpdateFailures=3 # Not supported ( DisableDeltaUpdateIfNeeded() )
kMaxConsecutiveObeyProxyRequests=20
kBroadcastThresholdProgress=0.01
kBroadcastThresholdSeconds=10
kAUTestURLRequest="autest"
kScheduledAUTestURLRequest="autest-scheduled"
last_notify_time_=
direct_proxy_resolver_=
chrome_proxy_resolver_=
processor_=
cert_checker_=
service_observers_=
install_plan_=() # import from omaha_response_handler_action
error_event_=
fake_update_success_="false"
http_response_code_=0
attempt_error_code_=0
cpu_limiter_=
# For status:
status_=$IDLE
download_progress_=0.0
last_checked_time_=0
prev_version_=
new_version_="0.0.0.0"
new_payload_size_=0
update_attempt_flags_=$kNone
current_update_attempt_flags_=$kNone
proxy_manual_checks_=0
obeying_proxies_="true"
is_install_="false"
forced_app_version_=
forced_omaha_url_=
#
# UpdateAttempter::RebootDirectly
#
function UpdateAttempter_RebootDirectly {
if "/sbin/shutdown" -r now; then echo "true"; else echo "false"; fi
}
#
# UpdateAttempter::RebootIfNeeded
#
function UpdateAttempter_RebootIfNeeded {
if [ "$(PowerManagerChromeOS_RequestReboot)" == "true" ]; then echo "true"; return 0; fi
UpdateAttempter_RebootDirectly
}
#
# UpdateAttempter::GetBootTimeAtUpdate
#
function UpdateAttempter_GetBootTimeAtUpdate {
local current_boot_id="$(Prefs_GetKey $kPrefsBootId)"
local update_completed_on_boot_id="$(Prefs_GetKey $kPrefsUpdateCompletedOnBootId)"
if [ -z "$current_boot_id" ] || [ -z $update_completed_on_boot_id ] || ! [ "$current_boot_id" == "$update_completed_on_boot_id" ]; then
echo "0"
return 0
fi
echo "$(Prefs_GetKey $kPrefsUpdateCompletedBootTime)"
}
#
# UpdateAttempter::ResetStatus
#
function UpdateAttempter_ResetStatus {
case $status_ in
$IDLE)
return 0
;;
$UPDATED_NEED_REBOOT)
status_=$IDLE
Prefs_Delete $kPrefsUpdateCompletedOnBootId
Prefs_Delete $kPrefsUpdateCompletedBootTime
BootControlChromeOS_Init
if BootControlChromeOS_SetActiveBootSlot $current_slot_; then
return 1
fi
# PayloadState::ResetUpdateStatus
Prefs_Delete $kPrefsTargetVersionInstalledFrom
local target_attempt=$(Prefs_GetKey $kPrefsTargetVersionAttempt)
Prefs_SetKey $kPrefsTargetVersionAttempt "$(( target_attempt-1 ))"
Prefs_SetKey $kPrefsPreviousVersion ""
return 0
;;
*)
>&2 echo "Reset not allowed in this state."
return 1
;;
esac
}
#
# UpdateAttempter::GetStatus
#
function UpdateAttempter_GetStatus {
last_checked_time=${last_checked_time_:-0}
status=${status_:-$IDLE}
current_version="${ImageProperties['version']}"
progress=${download_progress_:-0.0}
new_size_bytes=${new_payload_size_:-0}
new_version=${new_version_:-'0.0.0.0'}
is_enterprise_rollback="${install_plan['is_rollback']}"
is_enterprise_rollback="${is_enterprise_rollback:-false}"
is_install=${is_install_:-'false'}
eol_date=$(Prefs_GetKey $kPrefsOmahaEolDate)
eol_date=${eol_date:-0}
return 0
}
#
# UpdateAttempter::GetRollbackSlot
#
function UpdateAttempter_GetRollbackSlot {
BootControlChromeOS_Init
local num_slots=$num_slots_
local current_slot=$current_slot_
if [ "${current_slot}" == "${kInvalidSlot}" ] || [ $(( num_slots < 2 )) -eq 1 ]; then echo $kInvalidSlot; return 1; fi
for (( slot=0 ; slot<num_slots ; ++slot )); do
if [ $slot -ne $current_slot ] && [ "$(BootControlChromeOS_IsSlotBootable $slot)" == "true" ]; then
echo $slot
return 0
fi
done
echo $kInvalidSlot
return 1
}
#
# UpdateAttempter::CanRollback
#
function UpdateAttempter_CanRollback {
if [ $status_ -eq $IDLE ] && [ $(UpdateAttempter_GetRollbackSlot) -ne $kInvalidSlot ]; then
echo "true"
else
echo "false"
fi
}
#
# UpdateAttempter::IsAnyUpdateSourceAllowed
#
function UpdateAttempter_IsAnyUpdateSourceAllowed {
. "${SCRIPT_DIR}/hardware_chromeos.sh"
if [ "$(IsOfficialBuild)" == "false" ]; then
echo "true"
return 0
fi
if [ "$(AreDevFeaturesEnabled)" == "true" ]; then
echo "true"
return 0
fi
echo "false"
return 1
}
#
# UpdateAttempter::CalculateUpdateParams
#
function UpdateAttempter_CalculateUpdateParams {
# TODO: RefreshDevicePolicy
# TODO: UpdateRollbackHappened
# TODO: target_version_prefix_=
# TODO: rollback_allowed_=
# TODO: rollback_data_save_requested_=
# TODO: CalculateStagingParams
# TODO: CalculateScatteringParams
# TODO: rollback_allowed_milestones_=
# NO P2P!!
OmahaRequestParams_Init "$forced_app_version_" "$forced_omaha_url_" "$interactive"
# TODO: Set target channel from policy
# No support for DLC, so not going to CalculateDlcParams()
# is_install_ is already set
# TODO: add support for token
# No support for proxy
# Delta is disabled
return 0
}
#
# UpdateAttempter::BuildUpdateActions
# FIXME: This method is not properly implemented yet
# TODO: replace `return 1` with status messages
function UpdateAttempter_BuildUpdateActions {
# TODO: processor_->IsRunning()
# Check for update and apply if available
. "$SCRIPT_DIR/omaha_request_action.sh"
. "$SCRIPT_DIR/omaha_response_handler_action.sh"
. "$SCRIPT_DIR/download_action.sh"
. "$SCRIPT_DIR/delta_performer.sh"
OmahaRequestAction_TransferComplete || return 1
# Send signal: UPDATE_AVAILABLE?
if [ ${ORA_update_exists} ]; then
status_=${UPDATE_AVAILABLE}
new_payload_size_=${ORA_size:-0}
new_version_=${ORA_version:-'0.0.0.0'}
UpdateAttempter_GetStatus
BroadcastStatus "$last_checked_time" "$progress" "$status" "$new_version" "$new_size_bytes" "$is_enterprise_rollback" "$is_install" "$eol_date"
echo $(UpdateStatusToString $status)
else
status_=${REPORTING_ERROR_EVENT}
UpdateAttempter_GetStatus
BroadcastStatus "$last_checked_time" "$progress" "$status" "$new_version" "$new_size_bytes" "$is_enterprise_rollback" "$is_install" "$eol_date"
echo $(UpdateStatusToString $status)
return 1
fi
if ! $is_install; then
return 0
fi
OmahaResponseHandlerAction_PerformAction || return 1
# TODO: Run checks
# Send signal: DOWNLOADING
status_=${DOWNLOADING}
UpdateAttempter_GetStatus
BroadcastStatus "$last_checked_time" "$progress" "$status" "$new_version" "$new_size_bytes" "$is_enterprise_rollback" "$is_install" "$eol_date"
echo $(UpdateStatusToString $status)
DownloadAction_PerformAction || return 1
# TODO: Check if the update is downloaded properly
# Send signal: VERIFYING
status_=${VERIFYING}
UpdateAttempter_GetStatus
BroadcastStatus "$last_checked_time" "$progress" "$status" "$new_version" "$new_size_bytes" "$is_enterprise_rollback" "$is_install" "$eol_date"
echo $(UpdateStatusToString $status)
DownloadAction_TransferComplete || return 1
# TODO: Check if the verification is completed properly
# Send signal: FINALIZING
status_=${FINALIZING}
UpdateAttempter_GetStatus
BroadcastStatus "$last_checked_time" "$progress" "$status" "$new_version" "$new_size_bytes" "$is_enterprise_rollback" "$is_install" "$eol_date"
echo $(UpdateStatusToString $status)
PostinstallRunnerAction_PerformAction || return 1
if [ ${PostinstallRunnerAction_update_complete} ]; then
status_=${UPDATED_NEED_REBOOT}
UpdateAttempter_GetStatus
BroadcastStatus "$last_checked_time" "$progress" "$status" "$new_version" "$new_size_bytes" "$is_enterprise_rollback" "$is_install" "$eol_date"
echo $(UpdateStatusToString $status)
else
status_=${REPORTING_ERROR_EVENT}
UpdateAttempter_GetStatus
BroadcastStatus "$last_checked_time" "$progress" "$status" "$new_version" "$new_size_bytes" "$is_enterprise_rollback" "$is_install" "$eol_date"
echo $(UpdateStatusToString $status)
return 1
fi
return 0
}
#
# UpdateAttempter::Update
#
function UpdateAttempter_Update {
if [ "${status_}" == "${UPDATED_NEED_REBOOT}" ]; then
>&2 echo "Not updating b/c we already updated and we're waiting for reboot, we'll ping Omaha instead"
# TODO: UpdateAttempter_PingOmaha
return 1
fi
if ! [ "${status_}" == "${IDLE}" ]; then
return 1
fi
# Send signal: CHECKING_FOR_UPDATE
status_=${CHECKING_FOR_UPDATE}
UpdateAttempter_GetStatus
BroadcastStatus "$last_checked_time" "$progress" "$status" "$new_version" "$new_size_bytes" "$is_enterprise_rollback" "$is_install" "$eol_date"
echo $(UpdateStatusToString $status)
if ! UpdateAttempter_CalculateUpdateParams; then
return 1
fi
UpdateAttempter_BuildUpdateActions
# Send signal: UPDATED_NEED_REBOOT
status_=${UPDATED_NEED_REBOOT}
UpdateAttempter_GetStatus
BroadcastStatus "$last_checked_time" "$progress" "$status" "$new_version" "$new_size_bytes" "$is_enterprise_rollback" "$is_install" "$eol_date"
echo $(UpdateStatusToString $status)
}
#
# UpdateAttempter::OnUpdateScheduled
#
function UpdateAttempter_OnUpdateScheduled {
# TODO: UpdateManager/policy
# For now, just call Update
UpdateAttempter_Update
}
#
# UpdateAttempter::CheckForUpdate
# Args: APP_VERSION OMAHA_URL FLAGS
function UpdateAttempter_CheckForUpdate {
local app_version="$1"
local omaha_url="$2"
local flags="$3"
# Send signal: default status
#UpdateAttempter_GetStatus
#BroadcastStatus "$last_checked_time" "$progress" "$status" "$new_version" "$new_size_bytes" "$is_enterprise_rollback" "$is_install" "$eol_date"
if ! [ "${status_}" == "${IDLE}" ]; then
>&2 echo "Refusing to do an update as there already an update/install in progress"
return 1
fi
local interactive=$(( ! ( flags & kFlagNonInteractive ) ))
is_install_="false"
forced_app_version_=
forced_omaha_url_=
if [ "$(UpdateAttempter_IsAnyUpdateSourceAllowed)" == "true" ]; then
forced_app_version_=$app_version
forced_omaha_url_=$omaha_url
fi
if [ "${omaha_url}" == "${kScheduledAUTestURLRequest}" ]; then
forced_omaha_url_=${kOmahaDefaultAUTestURL}
interactive=0
elif [ "${omaha_url}" == "${kAUTestURLRequest}" ]; then
forced_omaha_url_=${kOmahaDefaultAUTestURL}
fi
if [ $interactive -eq 1 ]; then
current_update_attempt_flags_="$flags"
fi
# No need for forced_update_pending_callback_ or UpdateAttempter::ScheduleUpdates() or UpdateManager
# since we're checking for update immediately
UpdateAttempter_OnUpdateScheduled
}
#
# UpdateAttempter::Init
#
function UpdateAttempter_Init {
Prefs_Init
OmahaRequestParams_Init
# TODO: cert_checker_
if ! [ "$(UpdateAttempter_GetBootTimeAtUpdate)" == "0" ]; then
status_=$UPDATED_NEED_REBOOT
else
status_=$IDLE
fi
is_install_="false"
# UpdateAttempter::UpdateEngineStarted
# TODO:
}