swupdate.bbclass 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. # Copyright (C) 2015 Stefano Babic <sbabic@denx.de>
  2. #
  3. # Some parts from the patch class
  4. #
  5. # swupdate allows to generate a compound image for the
  6. # in the "swupdate" format, used for updating the targets
  7. # in field.
  8. # See also http://sbabic.github.io/swupdate/
  9. #
  10. #
  11. # To use, add swupdate to the inherit clause and set
  12. # set the images (all of them must be found in deploy directory)
  13. # that are part of the compound image.
  14. S = "${WORKDIR}/${PN}"
  15. IMAGE_DEPENDS ?= ""
  16. def swupdate_is_hash_needed(s, filename):
  17. with open(os.path.join(s, "sw-description"), 'r') as f:
  18. for line in f:
  19. if line.find("@%s" % (filename)) != -1:
  20. return True
  21. return False
  22. def swupdate_get_sha256(s, filename):
  23. import hashlib
  24. m = hashlib.sha256()
  25. with open(os.path.join(s, filename), 'rb') as f:
  26. while True:
  27. data = f.read(1024)
  28. if not data:
  29. break
  30. m.update(data)
  31. return m.hexdigest()
  32. def swupdate_write_sha256(s, filename, hash):
  33. write_lines = []
  34. with open(os.path.join(s, "sw-description"), 'r') as f:
  35. for line in f:
  36. write_lines.append(line.replace("@%s" % (filename), hash))
  37. with open(os.path.join(s, "sw-description"), 'w+') as f:
  38. for line in write_lines:
  39. f.write(line)
  40. def swupdate_getdepends(d):
  41. def adddep(depstr, deps):
  42. for i in (depstr or "").split():
  43. if i not in deps:
  44. deps.append(i)
  45. deps = []
  46. images = (d.getVar('IMAGE_DEPENDS', True) or "").split()
  47. for image in images:
  48. adddep(image , deps)
  49. depstr = ""
  50. for dep in deps:
  51. depstr += " " + dep + ":do_populate_sysroot"
  52. return depstr
  53. do_swuimage[dirs] = "${TOPDIR}"
  54. do_swuimage[cleandirs] += "${S}"
  55. do_swuimage[umask] = "022"
  56. do_configure[noexec] = "1"
  57. do_compile[noexec] = "1"
  58. do_install[noexec] = "1"
  59. do_package[noexec] = "1"
  60. do_package_qa[noexec] = "1"
  61. do_packagedata[noexec] = "1"
  62. do_package_write_ipk[noexec] = "1"
  63. do_package_write_deb[noexec] = "1"
  64. do_package_write_rpm[noexec] = "1"
  65. python () {
  66. deps = " " + swupdate_getdepends(d)
  67. d.appendVarFlag('do_build', 'depends', deps)
  68. }
  69. do_install () {
  70. }
  71. do_createlink () {
  72. cd ${DEPLOY_DIR_IMAGE}
  73. ln -sf ${IMAGE_NAME}.swu ${IMAGE_LINK_NAME}.swu
  74. }
  75. python do_swuimage () {
  76. import shutil
  77. workdir = d.getVar('WORKDIR', True)
  78. images = (d.getVar('SWUPDATE_IMAGES', True) or "").split()
  79. s = d.getVar('S', True)
  80. shutil.copyfile(os.path.join(workdir, "sw-description"), os.path.join(s, "sw-description"))
  81. fetch = bb.fetch2.Fetch([], d)
  82. list_for_cpio = ["sw-description"]
  83. for url in fetch.urls:
  84. local = fetch.localpath(url)
  85. filename = os.path.basename(local)
  86. shutil.copyfile(local, os.path.join(s, "%s" % filename ))
  87. if (filename != 'sw-description'):
  88. list_for_cpio.append(filename)
  89. deploydir = d.getVar('DEPLOY_DIR_IMAGE', True)
  90. for image in images:
  91. imagename = image + '-' + d.getVar('MACHINE', True)
  92. fstypes = (d.getVarFlag("SWUPDATE_IMAGES_FSTYPES", image, True) or "").split()
  93. for fstype in fstypes:
  94. imagebase = image + '-' + d.getVar('MACHINE', True)
  95. imagename = imagebase + fstype
  96. src = os.path.join(deploydir, "%s" % imagename)
  97. dst = os.path.join(s, "%s" % imagename)
  98. shutil.copyfile(src, dst)
  99. list_for_cpio.append(imagename)
  100. for file in list_for_cpio:
  101. if file != 'sw-description' and swupdate_is_hash_needed(s, file):
  102. hash = swupdate_get_sha256(s, file)
  103. swupdate_write_sha256(s, file, hash)
  104. line = 'for i in ' + ' '.join(list_for_cpio) + '; do echo $i;done | cpio -ov -H crc >' + os.path.join(deploydir,d.getVar('IMAGE_NAME', True) + '.swu')
  105. os.system("cd " + s + ";" + line)
  106. }
  107. COMPRESSIONTYPES = ""
  108. PACKAGE_ARCH = "${MACHINE_ARCH}"
  109. addtask do_swuimage after do_unpack before do_install
  110. addtask do_createlink after do_swuimage before do_install