Skip to content

Commit

Permalink
Expose names for bundled container images (#1357)
Browse files Browse the repository at this point in the history
* Start adding names for container images

* Update gold

* Clear metadata to zero

* Use named arguments
  • Loading branch information
kasperl authored Jan 22, 2023
1 parent 7a01d7e commit a8dddfb
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 11 deletions.
10 changes: 7 additions & 3 deletions lib/system/api/containers.toit
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,13 @@ class ContainerServiceClient extends ServiceClient implements ContainerService:

list_images -> List:
array := invoke_ ContainerService.LIST_IMAGES_INDEX null
return List array.size / 3:
cursor := it * 3
ContainerImage (uuid.Uuid array[cursor]) array[cursor + 1] array[cursor + 2]
return List array.size / 4:
cursor := it * 4
ContainerImage
--id=uuid.Uuid array[cursor]
--name=array[cursor + 1]
--flags=array[cursor + 2]
--data=array[cursor + 3]

load_image id/uuid.Uuid -> int?:
return invoke_ ContainerService.LOAD_IMAGE_INDEX id.to_byte_array
Expand Down
3 changes: 2 additions & 1 deletion lib/system/containers.toit
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ uninstall id/uuid.Uuid -> none:

class ContainerImage:
id/uuid.Uuid
name/string?
flags/int
data/int
constructor .id .flags .data:
constructor --.id --.name --.flags --.data:

class Container extends ServiceResourceProxy:
id/uuid.Uuid
Expand Down
2 changes: 1 addition & 1 deletion src/flash_allocation.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ class FlashAllocation {
memcpy(id_, id, id_size());
}
pages_in_flash_ = 0;
memset(_metadata, 0xFF, METADATA_SIZE);
memset(_metadata, 0, METADATA_SIZE);
set_uuid(uuid);
}

Expand Down
13 changes: 11 additions & 2 deletions system/containers.toit
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@
import binary
import uuid
import monitor
import encoding.base64 as base64

import encoding.base64
import encoding.tison

import system.assets
import system.services show ServiceDefinition ServiceResource
import system.api.containers show ContainerService

Expand Down Expand Up @@ -208,10 +211,16 @@ abstract class ContainerServiceDefinition extends ServiceDefinition
abstract lookup_image id/uuid.Uuid -> ContainerImage?

list_images -> List:
names := {:}
assets.decode.get "images" --if_present=: | encoded |
map := tison.decode encoded
map.do: | name/string id/ByteArray | names[uuid.Uuid id] = name
raw := images
result := []
raw.do: | image/ContainerImage |
result.add image.id.to_byte_array
id/uuid.Uuid := image.id
result.add id.to_byte_array
result.add (names.get id)
result.add image.flags
result.add image.data
return result
Expand Down
2 changes: 1 addition & 1 deletion tests/negative/gold/illegal_system_call_test.gold
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
As check failed: null is not a ServiceResource.
0: ServiceDefinition.resource <sdk>/system/services.toit:170:5
1: ContainerServiceDefinition.handle <...>/system/containers.toit:198:19
1: ContainerServiceDefinition.handle <...>/system/containers.toit:201:19
2: ServiceManager_.<lambda> <sdk>/system/services.toit:357:15
3: RpcRequest_.process.<block> <sdk>/rpc/broker.toit:98:26
4: RpcRequest_.process <sdk>/rpc/broker.toit:95:3
Expand Down
21 changes: 18 additions & 3 deletions tools/firmware.toit
Original file line number Diff line number Diff line change
Expand Up @@ -566,9 +566,24 @@ extract_binary envelope/Envelope --config_encoded/ByteArray -> ByteArray:
system := entries.get SYSTEM_CONTAINER_NAME
if system:
// TODO(kasper): Take any other system assets into account.
assets_encoded := properties.get "wifi"
--if_present=: assets.encode { "wifi": tison.encode it }
--if_absent=: null
system_assets := {:}
// Encode any WiFi information.
properties.get "wifi" --if_present=: system_assets["wifi"] = tison.encode it
// Encode the list of images with their names.
images := {:}
entries.do: | name/string content/ByteArray |
if not (name == SYSTEM_CONTAINER_NAME or name.starts_with "\$" or name.starts_with "+"):
id/uuid.Uuid := ?
if is_snapshot_bundle content:
bundle := SnapshotBundle name content
id = bundle.uuid
else:
header := decode_image content
id = header.id
images[name] = id.to_byte_array
if not images.is_empty: system_assets["images"] = tison.encode images
// Encode the system assets and add them to the container.
assets_encoded := assets.encode system_assets
containers.add (ContainerEntry SYSTEM_CONTAINER_NAME system --assets=assets_encoded)

entries.do: | name/string content/ByteArray |
Expand Down

0 comments on commit a8dddfb

Please sign in to comment.