Skip to content

Commit

Permalink
fix: bundle.bbclass: varflags expansion
Browse files Browse the repository at this point in the history
Python anonymous functions are evaluated before the `=` sign and Python tasks
do not expand variables. This fix introduces a set of varflags via
`RAUC_VARFLAGS_SLOTS` and `RAUC_VARFLAGS_HOOKS` variables.
It sets the `getVarFlags` keyword argument `expand` to
the respective list (default is `False`). This allows for assigning
varflags using bitbake variables (`${...}`) or Python inline syntax (`${@...}`).

Signed-off-by: Jean-Pierre Geslin <[email protected]>
  • Loading branch information
Jarsop committed Nov 10, 2023
1 parent 35b818b commit dbd96fc
Showing 1 changed file with 34 additions and 46 deletions.
80 changes: 34 additions & 46 deletions classes-recipe/bundle.bbclass
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,16 @@ RAUC_CASYNC_BUNDLE ??= "0"
RAUC_BUNDLE_FORMAT ??= ""
RAUC_BUNDLE_FORMAT[doc] = "Specifies the bundle format to be used (plain/verity)."

RAUC_VARFLAGS_SLOTS = "name type fstype file hooks adaptive rename offset depends"
RAUC_VARFLAGS_HOOKS = "file hooks"

# Create dependency list from images
python __anonymous() {
d.appendVarFlag('do_unpack', 'vardeps', ' RAUC_BUNDLE_HOOKS')
for slot in (d.getVar('RAUC_BUNDLE_SLOTS') or "").split():
slotflags = d.getVarFlags('RAUC_SLOT_%s' % slot)
imgtype = slotflags.get('type') if slotflags else None
slot_varflags = d.getVar('RAUC_VARFLAGS_SLOTS').split()
slotflags = d.getVarFlags('RAUC_SLOT_%s' % slot, expand=slot_varflags) or {}
imgtype = slotflags.get('type')
if not imgtype:
bb.debug(1, "No [type] given for slot '%s', defaulting to 'image'" % slot)
imgtype = 'image'
Expand All @@ -154,7 +158,7 @@ python __anonymous() {
return

d.appendVarFlag('do_unpack', 'vardeps', ' RAUC_SLOT_%s' % slot)
depends = slotflags.get('depends') if slotflags else None
depends = slotflags.get('depends')
if depends:
d.appendVarFlag('do_unpack', 'depends', ' ' + depends)
continue
Expand Down Expand Up @@ -221,7 +225,8 @@ def write_manifest(d):
'\nIf you are unsure, set RAUC_BUNDLE_FORMAT = "verity" for new projects.'
'\nRefer to https://rauc.readthedocs.io/en/latest/reference.html#sec-ref-formats for more information about RAUC bundle formats.')

hooksflags = d.getVarFlags('RAUC_BUNDLE_HOOKS')
hooks_varflags = d.getVar('RAUC_VARFLAGS_HOOKS').split()
hooksflags = d.getVarFlags('RAUC_BUNDLE_HOOKS', expand=hooks_varflags) or {}
have_hookfile = False
if 'file' in hooksflags:
have_hookfile = True
Expand All @@ -234,55 +239,38 @@ def write_manifest(d):
bb.warn("Suspicious use of RAUC_BUNDLE_HOOKS[hooks] without RAUC_BUNDLE_HOOKS[file]")

for slot in (d.getVar('RAUC_BUNDLE_SLOTS') or "").split():
slotflags = d.getVarFlags('RAUC_SLOT_%s' % slot)
if slotflags and 'name' in slotflags:
slotname = slotflags.get('name')
else:
slotname = slot
slot_varflags = d.getVar('RAUC_VARFLAGS_SLOTS').split()
slotflags = d.getVarFlags('RAUC_SLOT_%s' % slot, expand=slot_varflags) or {}

slotname = slotflags.get('name', slot)
manifest.write('[image.%s]\n' % slotname)
if slotflags and 'type' in slotflags:
imgtype = slotflags.get('type')
else:
imgtype = 'image'

if slotflags and 'fstype' in slotflags:
img_fstype = slotflags.get('fstype')
else:
img_fstype = d.getVar('RAUC_IMAGE_FSTYPE')
imgtype = slotflags.get('type', 'image')

img_fstype = slotflags.get('fstype', d.getVar('RAUC_IMAGE_FSTYPE'))

if imgtype == 'image':
if slotflags and 'file' in slotflags:
imgsource = d.getVarFlag('RAUC_SLOT_%s' % slot, 'file')
else:
imgsource = "%s-%s.rootfs.%s" % (d.getVar('RAUC_SLOT_%s' % slot), machine, img_fstype)
imgname = imgsource
fallback = "%s-%s.rootfs.%s" % (d.getVar('RAUC_SLOT_%s' % slot), machine, img_fstype)
imgname = imgsource = slotflags.get('file', fallback)
elif imgtype == 'kernel':
# TODO: Add image type support
if slotflags and 'file' in slotflags:
imgsource = d.getVarFlag('RAUC_SLOT_%s' % slot, 'file')
else:
imgsource = "%s-%s.bin" % ("zImage", machine)
fallback = "%s-%s.bin" % ("zImage", machine)
imgsource = slotflags.get('file', fallback)
imgname = "%s.%s" % (imgsource, "img")
elif imgtype == 'boot':
if slotflags and 'file' in slotflags:
imgsource = d.getVarFlag('RAUC_SLOT_%s' % slot, 'file')
else:
imgsource = "%s" % ("barebox.img")
imgname = imgsource
imgname = imgsource = slotflags.get('file', 'barebox.img')
elif imgtype == 'file':
if slotflags and 'file' in slotflags:
imgsource = d.getVarFlag('RAUC_SLOT_%s' % slot, 'file')
else:
imgsource = slotflags.get('file')
if not imgsource:
bb.fatal('Unknown file for slot: %s' % slot)
imgname = "%s.%s" % (imgsource, "img")
else:
bb.fatal('Unknown image type: %s' % imgtype)

if slotflags and 'rename' in slotflags:
imgname = d.getVarFlag('RAUC_SLOT_%s' % slot, 'rename')
if slotflags and 'offset' in slotflags:
imgname = slotflags.get('rename', imgname)
if 'offset' in slotflags:
padding = 'seek'
imgoffset = d.getVarFlag('RAUC_SLOT_%s' % slot, 'offset')
imgoffset = slotflags.get('offset')
if imgoffset:
sign, magnitude = imgoffset[:1], imgoffset[1:]
if sign == '+':
Expand All @@ -297,12 +285,12 @@ def write_manifest(d):
# Keep only the image name in case the image is in a $DEPLOY_DIR_IMAGE subdirectory
imgname = PurePath(imgname).name
manifest.write("filename=%s\n" % imgname)
if slotflags and 'hooks' in slotflags:
if 'hooks' in slotflags:
if not have_hookfile:
bb.warn("A hook is defined for slot %s, but RAUC_BUNDLE_HOOKS[file] is not defined" % slot)
manifest.write("hooks=%s\n" % d.getVarFlag('RAUC_SLOT_%s' % slot, 'hooks'))
if slotflags and 'adaptive' in slotflags:
manifest.write("adaptive=%s\n" % d.getVarFlag('RAUC_SLOT_%s' % slot, 'adaptive'))
manifest.write("hooks=%s\n" % slotflags.get('hooks'))
if 'adaptive' in slotflags:
manifest.write("adaptive=%s\n" % slotflags.get('adaptive'))
manifest.write("\n")

bundle_imgpath = "%s/%s" % (bundle_path, imgname)
Expand All @@ -327,8 +315,7 @@ def write_manifest(d):

for meta_section in (d.getVar('RAUC_META_SECTIONS') or "").split():
manifest.write("[meta.%s]\n" % meta_section)
for meta_key in d.getVarFlags('RAUC_META_%s' % meta_section):
meta_value = d.getVarFlag('RAUC_META_%s' % meta_section, meta_key)
for meta_key, meta_value in d.getVarFlags('RAUC_META_%s' % meta_section).items():
manifest.write("%s=%s\n" % (meta_key, meta_value))
manifest.write("\n");

Expand Down Expand Up @@ -363,8 +350,9 @@ python do_configure() {

write_manifest(d)

hooksflags = d.getVarFlags('RAUC_BUNDLE_HOOKS')
if hooksflags and 'file' in hooksflags:
hooks_varflags = d.getVar('RAUC_VARFLAGS_HOOKS').split()
hooksflags = d.getVarFlags('RAUC_BUNDLE_HOOKS', expand=hooks_varflags) or {}
if 'file' in hooksflags:
hf = hooksflags.get('file')
if not os.path.exists(d.expand("${WORKDIR}/%s" % hf)):
bb.error("hook file '%s' does not exist in WORKDIR" % hf)
Expand Down

0 comments on commit dbd96fc

Please sign in to comment.