swupdate-common.bbclass 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. # Copyright (C) 2015-2022 Stefano Babic
  2. #
  3. # SPDX-License-Identifier: GPLv3
  4. inherit swupdate-lib
  5. S = "${WORKDIR}/sources"
  6. UNPACKDIR = "${S}"
  7. DEPENDS += "\
  8. cpio-native \
  9. ${@ 'openssl-native' if d.getVar('SWUPDATE_SIGNING') or d.getVar('SWUPDATE_ENCRYPT_SWDESC') or d.getVarFlags('SWUPDATE_IMAGES_ENCRYPTED') else ''} \
  10. "
  11. do_swuimage[umask] = "022"
  12. SSTATETASKS += "do_swuimage"
  13. SSTATE_SKIP_CREATION:task-swuimage = '1'
  14. SWUDEPLOYDIR = "${WORKDIR}/deploy-${PN}-swuimage"
  15. do_swuimage[dirs] = "${SWUDEPLOYDIR}"
  16. do_swuimage[cleandirs] += "${SWUDEPLOYDIR}"
  17. do_swuimage[sstate-inputdirs] = "${SWUDEPLOYDIR}"
  18. do_swuimage[sstate-outputdirs] = "${DEPLOY_DIR_IMAGE}"
  19. do_swuimage[stamp-extra-info] = "${MACHINE}"
  20. python () {
  21. deps = " " + swupdate_getdepends(d)
  22. d.appendVarFlag('do_swuimage', 'depends', deps)
  23. d.delVarFlag('do_fetch', 'noexec')
  24. d.delVarFlag('do_unpack', 'noexec')
  25. }
  26. def get_pwd_file_args(d, passfile):
  27. pwd_args = []
  28. pwd_file = d.getVar(passfile)
  29. if pwd_file:
  30. pwd_args = ["-passin", "file:%s" % pwd_file]
  31. return pwd_args
  32. def get_certfile_args(d):
  33. extra_certs = d.getVar('SWUPDATE_CMS_EXTRA_CERTS')
  34. if not extra_certs:
  35. return []
  36. certfile_args = []
  37. extra_paths = extra_certs.split()
  38. for crt_path in extra_paths:
  39. if not os.path.exists(crt_path):
  40. bb.fatal("SWUPDATE_CMS_EXTRA_CERTS path %s doesn't exist" % (crt_path))
  41. certfile_args.extend(["-certfile", crt_path])
  42. return certfile_args
  43. def swupdate_getdepends(d):
  44. def adddep(depstr, deps):
  45. for i in (depstr or "").split():
  46. if i not in deps:
  47. deps.append(i)
  48. deps = []
  49. images = (d.getVar('IMAGE_DEPENDS') or "").split()
  50. for image in images:
  51. adddep(image , deps)
  52. depstr = ""
  53. for dep in deps:
  54. depstr += " " + dep + ":do_build"
  55. return depstr
  56. def swupdate_write_sha256(s):
  57. import re
  58. write_lines = []
  59. with open(os.path.join(s, "sw-description"), 'r') as f:
  60. for line in f:
  61. shastr = r"sha256.+=.+@(.+\")"
  62. m = re.match(r"^(?P<before_placeholder>.+)(sha256|version).+[=:].*(?P<quote>[\'\"])@(?P<filename>.*)(?P=quote)", line)
  63. if m:
  64. filename = m.group('filename')
  65. bb.warn("Syntax for sha256 changed, please use $swupdate_get_sha256(%s)" % filename)
  66. hash = swupdate_get_sha256(None, s, filename)
  67. write_lines.append(line.replace("@%s" % (filename), hash))
  68. else:
  69. write_lines.append(line)
  70. with open(os.path.join(s, "sw-description"), 'w+') as f:
  71. for line in write_lines:
  72. f.write(line)
  73. def swupdate_exec_functions(d, s, write_lines):
  74. import re
  75. for index, line in enumerate(write_lines):
  76. m = re.match(r"^(?P<before_placeholder>.+)\$(?P<bitbake_function_name>\w+)\((?P<parms>.+)\)(?P<after_placeholder>.+)$", line)
  77. if m:
  78. fun = m.group('bitbake_function_name') + "(d, \"" + s + "\", \"" + m.group('parms') + "\")"
  79. ret = eval(fun)
  80. bb.debug (2, "%s return %s " % (m.group('bitbake_function_name'), ret))
  81. line = m.group('before_placeholder') + ret + m.group('after_placeholder') + "\n"
  82. write_lines[index] = line
  83. def swupdate_expand_bitbake_variables(d, s):
  84. write_lines = []
  85. with open(os.path.join(s, "sw-description"), 'r') as f:
  86. import re
  87. for line in f:
  88. found = False
  89. while True:
  90. m = re.match(r"^(?P<before_placeholder>.*)@@(?P<bitbake_variable_name>\w+)@@(?P<after_placeholder>.*)$", line)
  91. if m:
  92. bitbake_variable_value = d.getVar(m.group('bitbake_variable_name'), True)
  93. if bitbake_variable_value is None:
  94. bitbake_variable_value = ""
  95. bb.warn("BitBake variable %s not set" % (m.group('bitbake_variable_name')))
  96. line = m.group('before_placeholder') + bitbake_variable_value + m.group('after_placeholder')
  97. found = True
  98. continue
  99. else:
  100. m = re.match(r"^(?P<before_placeholder>.*)@@(?P<bitbake_variable_name>.+)\[(?P<flag_var_name>.+)\]@@(?P<after_placeholder>.*)$", line)
  101. if m:
  102. bitbake_variable_value = (d.getVarFlag(m.group('bitbake_variable_name'), m.group('flag_var_name'), True) or "")
  103. if bitbake_variable_value is None:
  104. bitbake_variable_value = ""
  105. line = m.group('before_placeholder') + bitbake_variable_value + m.group('after_placeholder')
  106. continue
  107. if found:
  108. line = line + "\n"
  109. break
  110. write_lines.append(line)
  111. swupdate_exec_functions(d, s, write_lines)
  112. with open(os.path.join(s, "sw-description"), 'w+') as f:
  113. for line in write_lines:
  114. f.write(line)
  115. # Get all the variables referred by the sw-description at parse time.
  116. def swupdate_find_bitbake_variables(d):
  117. import re
  118. vardeps = []
  119. filespath = d.getVar('FILESPATH')
  120. sw_desc_path = bb.utils.which(filespath, "sw-description")
  121. try:
  122. with open(sw_desc_path, "r") as f:
  123. for line in f:
  124. found = False
  125. while True:
  126. m = re.match(r"^(?P<before_placeholder>.*)@@(?P<bitbake_variable_name>\w+)@@(?P<after_placeholder>.*)$", line)
  127. if m:
  128. bitbake_variable_value = m.group('bitbake_variable_name')
  129. vardeps.append(bitbake_variable_value)
  130. line = m.group('before_placeholder') + bitbake_variable_value + m.group('after_placeholder')
  131. found = True
  132. continue
  133. else:
  134. m = re.match(r"^(?P<before_placeholder>.*)@@(?P<bitbake_variable_name>.+)\[(?P<flag_var_name>.+)\]@@(?P<after_placeholder>.*)$", line)
  135. if m:
  136. bitbake_variable_value = m.group('bitbake_variable_name')
  137. vardeps.append(bitbake_variable_value)
  138. flag_name = m.group('flag_var_name')
  139. vardeps.append(flag_name)
  140. line = m.group('before_placeholder') + bitbake_variable_value + m.group('after_placeholder')
  141. continue
  142. break
  143. except IOError:
  144. pass
  145. return ' '.join(set(vardeps))
  146. def prepare_sw_description(d):
  147. import shutil
  148. import subprocess
  149. s = d.getVar('S')
  150. swupdate_expand_bitbake_variables(d, s)
  151. swupdate_write_sha256(s)
  152. encrypt = d.getVar('SWUPDATE_ENCRYPT_SWDESC')
  153. if encrypt:
  154. bb.note("Encryption of sw-description")
  155. shutil.copyfile(os.path.join(s, 'sw-description'), os.path.join(s, 'sw-description.plain'))
  156. key,iv = swupdate_extract_keys(d.getVar('SWUPDATE_AES_FILE'))
  157. iv = swupdate_get_IV(d, s, 'sw-description')
  158. swupdate_encrypt_file(os.path.join(s, 'sw-description.plain'), os.path.join(s, 'sw-description'), key, iv)
  159. signing = d.getVar('SWUPDATE_SIGNING')
  160. if signing == "1":
  161. bb.warn('SWUPDATE_SIGNING = "1" is deprecated, falling back to "RSA". It is advised to set it to "RSA" if using RSA signing.')
  162. signing = "RSA"
  163. if signing:
  164. sw_desc_sig = os.path.join(s, 'sw-description.sig')
  165. sw_desc = os.path.join(s, 'sw-description.plain' if encrypt else 'sw-description')
  166. if signing == "CUSTOM":
  167. signcmd = []
  168. sign_tool = d.getVar('SWUPDATE_SIGN_TOOL')
  169. signtool = sign_tool.split()
  170. for i in range(len(signtool)):
  171. signcmd.append(signtool[i])
  172. if not signcmd:
  173. bb.fatal("Custom SWUPDATE_SIGN_TOOL is not given")
  174. elif signing == "RSA":
  175. privkey = d.getVar('SWUPDATE_PRIVATE_KEY')
  176. if not privkey:
  177. bb.fatal("SWUPDATE_PRIVATE_KEY isn't set")
  178. if not os.path.exists(privkey):
  179. bb.fatal("SWUPDATE_PRIVATE_KEY %s doesn't exist" % (privkey))
  180. signcmd = ["openssl", "dgst", "-sha256", "-sign", privkey] + get_pwd_file_args(d, 'SWUPDATE_PASSWORD_FILE') + ["-out", sw_desc_sig, sw_desc]
  181. elif signing == "CMS":
  182. cms_cert = d.getVar('SWUPDATE_CMS_CERT')
  183. if not cms_cert:
  184. bb.fatal("SWUPDATE_CMS_CERT is not set")
  185. if not os.path.exists(cms_cert):
  186. bb.fatal("SWUPDATE_CMS_CERT %s doesn't exist" % (cms_cert))
  187. cms_key = d.getVar('SWUPDATE_CMS_KEY')
  188. if not cms_key:
  189. bb.fatal("SWUPDATE_CMS_KEY isn't set")
  190. if not os.path.exists(cms_key):
  191. bb.fatal("SWUPDATE_CMS_KEY %s doesn't exist" % (cms_key))
  192. signcmd = ["openssl", "cms", "-sign", "-in", sw_desc, "-out", sw_desc_sig, "-signer", cms_cert, "-inkey", cms_key] + \
  193. ["-outform", "DER", "-nosmimecap", "-binary"] + \
  194. get_pwd_file_args(d, 'SWUPDATE_PASSWORD_FILE') + \
  195. get_certfile_args(d)
  196. else:
  197. bb.fatal("Unrecognized SWUPDATE_SIGNING mechanism.")
  198. subprocess.run(' '.join(signcmd), shell=True, check=True)
  199. def swupdate_add_src_uri(d, list_for_cpio):
  200. import shutil
  201. s = d.getVar('S')
  202. exclude = (d.getVar("SWUPDATE_SRC_URI_EXCLUDE") or "").split()
  203. fetch = bb.fetch2.Fetch([], d)
  204. # Add files listed in SRC_URI to the swu file
  205. for url in fetch.urls:
  206. local = fetch.localpath(url)
  207. filename = os.path.basename(local)
  208. if filename in exclude:
  209. continue
  210. aes_file = d.getVar('SWUPDATE_AES_FILE')
  211. if aes_file:
  212. key,iv = swupdate_extract_keys(d.getVar('SWUPDATE_AES_FILE'))
  213. if (filename != 'sw-description') and (os.path.isfile(local)):
  214. encrypted = (d.getVarFlag("SWUPDATE_IMAGES_ENCRYPTED", filename) or "")
  215. dst = os.path.join(s, "%s" % filename )
  216. if encrypted == '1':
  217. bb.note("Encryption requested for %s" %(filename))
  218. if not key or not iv:
  219. bb.fatal("Encryption required, but no key found")
  220. iv = swupdate_get_IV(d, s, filename)
  221. swupdate_encrypt_file(local, dst, key, iv)
  222. else:
  223. shutil.copyfile(local, dst)
  224. list_for_cpio.append(filename)
  225. def add_image_to_swu(d, deploydir, imagename, s, encrypt, list_for_cpio):
  226. import shutil
  227. src = os.path.join(deploydir, imagename)
  228. if not os.path.isfile(src):
  229. return False
  230. target_imagename = os.path.basename(imagename) # allow images in subfolders of DEPLOY_DIR_IMAGE
  231. dst = os.path.join(s, target_imagename)
  232. if encrypt == '1':
  233. key,iv = swupdate_extract_keys(d.getVar('SWUPDATE_AES_FILE'))
  234. bb.note("Encryption requested for %s" %(imagename))
  235. iv = swupdate_get_IV(d, s, imagename)
  236. swupdate_encrypt_file(src, dst, key, iv)
  237. else:
  238. shutil.copyfile(src, dst)
  239. list_for_cpio.append(target_imagename)
  240. return True
  241. def swupdate_add_artifacts(d, list_for_cpio):
  242. import shutil
  243. # Search for images listed in SWUPDATE_IMAGES in the DEPLOY directory.
  244. images = (d.getVar('SWUPDATE_IMAGES') or "").split()
  245. deploydir = d.getVar('DEPLOY_DIR_IMAGE')
  246. imgdeploydir = d.getVar('SWUDEPLOYDIR')
  247. s = d.getVar('S')
  248. for image in images:
  249. fstypes = (d.getVarFlag("SWUPDATE_IMAGES_FSTYPES", image) or "").split()
  250. encrypted = (d.getVarFlag("SWUPDATE_IMAGES_ENCRYPTED", image) or "")
  251. if fstypes:
  252. noappend_machine = d.getVarFlag("SWUPDATE_IMAGES_NOAPPEND_MACHINE", image)
  253. if noappend_machine == "0": # Search for a file explicitly with MACHINE
  254. imagebases = [ image + '-' + d.getVar('MACHINE') ]
  255. elif noappend_machine == "1": # Search for a file explicitly without MACHINE
  256. imagebases = [ image ]
  257. else: # None, means auto mode. Just try to find an image file with MACHINE or without MACHINE
  258. imagebases = [ image + '-' + d.getVar('MACHINE'), image ]
  259. for fstype in fstypes:
  260. image_found = False
  261. for imagebase in imagebases:
  262. image_found = add_image_to_swu(d, deploydir, imagebase + fstype, s, encrypted, list_for_cpio)
  263. if image_found:
  264. break
  265. if not image_found:
  266. bb.fatal("swupdate cannot find image file: %s" % os.path.join(deploydir, imagebase + fstype))
  267. else: # Allow also complete entries like "image.ext4.gz" in SWUPDATE_IMAGES
  268. if not add_image_to_swu(d, deploydir, image, s, encrypted, list_for_cpio):
  269. bb.fatal("swupdate cannot find %s image file" % image)
  270. def swupdate_create_cpio(d, swudeploydir, list_for_cpio):
  271. s = d.getVar('S')
  272. os.chdir(s)
  273. updateimage = d.getVar('IMAGE_NAME') + '.swu'
  274. line = 'for i in ' + ' '.join(list_for_cpio) + '; do echo $i;done | cpio -ov -H crc --reproducible > ' + os.path.join(swudeploydir, updateimage)
  275. os.system(line)
  276. os.chdir(swudeploydir)
  277. updateimage_link = d.getVar('IMAGE_LINK_NAME')
  278. if updateimage_link:
  279. updateimage_link += '.swu'
  280. if updateimage_link != updateimage:
  281. os.symlink(updateimage, updateimage_link)
  282. python do_swuimage () {
  283. import shutil
  284. list_for_cpio = ["sw-description"]
  285. s = d.getVar('S')
  286. imgdeploydir = d.getVar('SWUDEPLOYDIR')
  287. if d.getVar('SWUPDATE_SIGNING'):
  288. list_for_cpio.append('sw-description.sig')
  289. # Add artifacts added via SRC_URI
  290. if not d.getVar('INHIBIT_SWUPDATE_ADD_SRC_URI'):
  291. swupdate_add_src_uri(d, list_for_cpio)
  292. # Add artifacts set via SWUPDATE_IMAGES
  293. swupdate_add_artifacts(d, list_for_cpio)
  294. prepare_sw_description(d)
  295. swupdate_create_cpio(d, imgdeploydir, list_for_cpio)
  296. }