]>
glassweightruler.freedombox.rocks Git - waydroid.git/blob - tools/helpers/images.py
1 # Copyright 2021 Erfan Abdi
2 # SPDX-License-Identifier: GPL-3.0-or-later
9 from tools
import helpers
12 def sha256sum(filename
):
14 b
= bytearray(128*1024)
16 with open(filename
, 'rb', buffering
=0) as f
:
17 for n
in iter(lambda: f
.readinto(mv
), 0):
23 cfg
= tools
.config
.load(args
)
24 system_ota
= cfg
["waydroid"]["system_ota"]
25 system_request
= helpers
.http
.retrieve(system_ota
)
26 if system_request
[0] != 200:
28 "Failed to get system OTA channel: {}, error: {}".format(args
.system_ota
, system_request
[0]))
29 system_responses
= json
.loads(system_request
[1].decode('utf8'))["response"]
30 if len(system_responses
) < 1:
31 raise ValueError("No images found on system channel")
33 for system_response
in system_responses
:
34 if system_response
['datetime'] > int(cfg
["waydroid"]["system_datetime"]):
35 images_zip
= helpers
.http
.download(
36 args
, system_response
['url'], system_response
['filename'], cache
=False)
37 logging
.info("Validating system image")
38 if sha256sum(images_zip
) != system_response
['id']:
43 raise ValueError("Downloaded system image hash doesn't match, expected: {}".format(
44 system_response
['id']))
45 logging
.info("Extracting to " + args
.images_path
)
46 with zipfile
.ZipFile(images_zip
, 'r') as zip_ref
:
47 zip_ref
.extractall(args
.images_path
)
48 cfg
["waydroid"]["system_datetime"] = str(system_response
['datetime'])
49 tools
.config
.save(args
, cfg
)
53 vendor_ota
= cfg
["waydroid"]["vendor_ota"]
54 vendor_request
= helpers
.http
.retrieve(vendor_ota
)
55 if vendor_request
[0] != 200:
57 "Failed to get vendor OTA channel: {}, error: {}".format(vendor_ota
, vendor_request
[0]))
58 vendor_responses
= json
.loads(vendor_request
[1].decode('utf8'))["response"]
59 if len(vendor_responses
) < 1:
60 raise ValueError("No images found on vendor channel")
62 for vendor_response
in vendor_responses
:
63 if vendor_response
['datetime'] > int(cfg
["waydroid"]["vendor_datetime"]):
64 images_zip
= helpers
.http
.download(
65 args
, vendor_response
['url'], vendor_response
['filename'], cache
=False)
66 logging
.info("Validating vendor image")
67 if sha256sum(images_zip
) != vendor_response
['id']:
72 raise ValueError("Downloaded vendor image hash doesn't match, expected: {}".format(
73 vendor_response
['id']))
74 logging
.info("Extracting to " + args
.images_path
)
75 with zipfile
.ZipFile(images_zip
, 'r') as zip_ref
:
76 zip_ref
.extractall(args
.images_path
)
77 cfg
["waydroid"]["vendor_datetime"] = str(vendor_response
['datetime'])
78 tools
.config
.save(args
, cfg
)
82 def replace(args
, system_zip
, system_time
, vendor_zip
, vendor_time
):
83 cfg
= tools
.config
.load(args
)
84 args
.images_path
= cfg
["waydroid"]["images_path"]
85 if os
.path
.exists(system_zip
):
86 with zipfile
.ZipFile(system_zip
, 'r') as zip_ref
:
87 zip_ref
.extractall(args
.images_path
)
88 cfg
["waydroid"]["system_datetime"] = str(system_time
)
89 tools
.config
.save(args
, cfg
)
90 if os
.path
.exists(vendor_zip
):
91 with zipfile
.ZipFile(vendor_zip
, 'r') as zip_ref
:
92 zip_ref
.extractall(args
.images_path
)
93 cfg
["waydroid"]["vendor_datetime"] = str(vendor_time
)
94 tools
.config
.save(args
, cfg
)
97 def mount_rootfs(args
, images_dir
):
98 helpers
.mount
.mount(args
, images_dir
+ "/system.img",
99 tools
.config
.defaults
["rootfs"], umount
=True)
100 helpers
.mount
.mount(args
, images_dir
+ "/vendor.img",
101 tools
.config
.defaults
["rootfs"] + "/vendor")
102 for egl_path
in ["/vendor/lib/egl", "/vendor/lib64/egl"]:
103 if os
.path
.isdir(egl_path
):
105 args
, egl_path
, tools
.config
.defaults
["rootfs"] + egl_path
)
106 if helpers
.mount
.ismount("/odm"):
108 args
, "/odm", tools
.config
.defaults
["rootfs"] + "/odm_extra")
110 if os
.path
.isdir("/vendor/odm"):
112 args
, "/vendor/odm", tools
.config
.defaults
["rootfs"] + "/odm_extra")
113 helpers
.mount
.bind_file(args
, args
.work
+ "/waydroid.prop",
114 tools
.config
.defaults
["rootfs"] + "/vendor/waydroid.prop")
116 def umount_rootfs(args
):
117 helpers
.mount
.umount_all(args
, tools
.config
.defaults
["rootfs"])