Andrew Scull | 1883487 | 2018-10-12 11:48:09 +0100 | [diff] [blame] | 1 | /* |
Andrew Walbran | 692b325 | 2019-03-07 15:51:31 +0000 | [diff] [blame] | 2 | * Copyright 2018 The Hafnium Authors. |
Andrew Scull | 1883487 | 2018-10-12 11:48:09 +0100 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Andrew Scull | 18c78fc | 2018-08-20 12:57:41 +0100 | [diff] [blame] | 17 | #include "hf/cpio.h" |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 18 | |
| 19 | #include <stdint.h> |
| 20 | |
Andrew Scull | 8d9e121 | 2019-04-05 13:52:55 +0100 | [diff] [blame] | 21 | #include "hf/std.h" |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 22 | |
| 23 | #pragma pack(push, 1) |
| 24 | struct cpio_header { |
| 25 | uint16_t magic; |
| 26 | uint16_t dev; |
| 27 | uint16_t ino; |
| 28 | uint16_t mode; |
| 29 | uint16_t uid; |
| 30 | uint16_t gid; |
| 31 | uint16_t nlink; |
| 32 | uint16_t rdev; |
| 33 | uint16_t mtime[2]; |
| 34 | uint16_t namesize; |
| 35 | uint16_t filesize[2]; |
| 36 | }; |
| 37 | #pragma pack(pop) |
| 38 | |
Wedson Almeida Filho | 9ee60e9 | 2018-07-23 18:56:56 +0100 | [diff] [blame] | 39 | /** |
| 40 | * Retrieves the next file stored in the cpio archive stored in the cpio, and |
| 41 | * advances the iterator such that another call to this function would return |
| 42 | * the following file. |
| 43 | */ |
David Brazdil | 136f294 | 2019-09-23 14:11:03 +0100 | [diff] [blame] | 44 | static bool cpio_next(struct memiter *iter, const char **name, |
| 45 | const void **contents, size_t *size) |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 46 | { |
Andrew Scull | 3c351e9 | 2020-01-28 11:26:05 +0000 | [diff] [blame^] | 47 | static const char trailer[] = "TRAILER!!!"; |
Wedson Almeida Filho | 9ee60e9 | 2018-07-23 18:56:56 +0100 | [diff] [blame] | 48 | size_t len; |
| 49 | struct memiter lit = *iter; |
| 50 | const struct cpio_header *h = (const struct cpio_header *)lit.next; |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 51 | |
Wedson Almeida Filho | 9ee60e9 | 2018-07-23 18:56:56 +0100 | [diff] [blame] | 52 | if (!memiter_advance(&lit, sizeof(struct cpio_header))) { |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 53 | return false; |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 54 | } |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 55 | |
Wedson Almeida Filho | 9ee60e9 | 2018-07-23 18:56:56 +0100 | [diff] [blame] | 56 | *name = lit.next; |
| 57 | |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 58 | /* TODO: Check magic. */ |
| 59 | |
Wedson Almeida Filho | 9ee60e9 | 2018-07-23 18:56:56 +0100 | [diff] [blame] | 60 | len = (h->namesize + 1) & ~1; |
| 61 | if (!memiter_advance(&lit, len)) { |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 62 | return false; |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 63 | } |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 64 | |
Wedson Almeida Filho | 9ee60e9 | 2018-07-23 18:56:56 +0100 | [diff] [blame] | 65 | *contents = lit.next; |
| 66 | |
| 67 | len = (size_t)h->filesize[0] << 16 | h->filesize[1]; |
| 68 | if (!memiter_advance(&lit, (len + 1) & ~1)) { |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 69 | return false; |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 70 | } |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 71 | |
| 72 | /* TODO: Check that string is null-terminated. */ |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 73 | |
| 74 | /* Stop enumerating files when we hit the end marker. */ |
Andrew Scull | 3c351e9 | 2020-01-28 11:26:05 +0000 | [diff] [blame^] | 75 | if (!strncmp(*name, trailer, sizeof(trailer))) { |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 76 | return false; |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 77 | } |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 78 | |
Wedson Almeida Filho | 9ee60e9 | 2018-07-23 18:56:56 +0100 | [diff] [blame] | 79 | *size = len; |
| 80 | *iter = lit; |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 81 | |
| 82 | return true; |
| 83 | } |
David Brazdil | 136f294 | 2019-09-23 14:11:03 +0100 | [diff] [blame] | 84 | |
| 85 | /** |
| 86 | * Looks for a file in the given cpio archive. The file, if found, is returned |
| 87 | * in the "it" argument. |
| 88 | */ |
| 89 | bool cpio_get_file(const struct memiter *cpio, const struct string *name, |
| 90 | struct memiter *it) |
| 91 | { |
| 92 | const char *fname; |
| 93 | const void *fcontents; |
| 94 | size_t fsize; |
| 95 | struct memiter iter = *cpio; |
| 96 | |
| 97 | while (cpio_next(&iter, &fname, &fcontents, &fsize)) { |
Andrew Scull | 3c351e9 | 2020-01-28 11:26:05 +0000 | [diff] [blame^] | 98 | if (!strncmp(fname, string_data(name), STRING_MAX_SIZE)) { |
David Brazdil | 136f294 | 2019-09-23 14:11:03 +0100 | [diff] [blame] | 99 | memiter_init(it, fcontents, fsize); |
| 100 | return true; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | return false; |
| 105 | } |