|
13 | 13 | #include "kernel/ext_manifest.h" |
14 | 14 | #include "rimage.h" |
15 | 15 |
|
| 16 | +#ifndef ALIGN_UP |
| 17 | +#define ALIGN_UP(size, alignment) \ |
| 18 | + ((size) + (((alignment) - ((size) % (alignment))) % (alignment))) |
| 19 | +#endif |
| 20 | + |
16 | 21 | const struct ext_man_header ext_man_template = { |
17 | 22 | .magic = EXT_MAN_MAGIC_NUMBER, |
18 | 23 | .header_version = EXT_MAN_VERSION, |
@@ -81,53 +86,129 @@ static int ext_man_validate(uint32_t section_size, const void *section_data) |
81 | 86 | } |
82 | 87 | } |
83 | 88 |
|
| 89 | +/* |
| 90 | + * Insert `section_name` content after `elem_type` header. |
| 91 | + * Save result in new buffer, then replace `meta_section_data`. |
| 92 | + * Return `section_name` size or error code. |
| 93 | + */ |
| 94 | +static int ext_man_update_element_with_list(const struct module *module, |
| 95 | + uint32_t* meta_section_size, |
| 96 | + uint8_t **meta_section_data, |
| 97 | + enum ext_man_elem_type elem_type, |
| 98 | + const char *section_name) |
| 99 | +{ |
| 100 | + struct ext_man_elem_header* head = NULL; |
| 101 | + uint8_t *msd_buff = *meta_section_data; |
| 102 | + uint8_t *sec_buffer = NULL; |
| 103 | + const Elf32_Shdr *section; |
| 104 | + uint32_t offset = 0; |
| 105 | + uint32_t extra_size; |
| 106 | + uint8_t *temp_buff; |
| 107 | + int ret; |
| 108 | + |
| 109 | + /* find `elem_type` in meta_section */ |
| 110 | + while (offset < *meta_section_size) { |
| 111 | + head = (struct ext_man_elem_header*)&msd_buff[offset]; |
| 112 | + if (head->type == elem_type || !head->elem_size) |
| 113 | + break; |
| 114 | + offset += head->elem_size; |
| 115 | + } |
| 116 | + |
| 117 | + if (!head || head->type != elem_type) { |
| 118 | + fprintf(stdout, |
| 119 | + "Extended manifest element %d not found\n", elem_type); |
| 120 | + return -EINVAL; |
| 121 | + } |
| 122 | + |
| 123 | + /* read section with list content */ |
| 124 | + ret = elf_read_section(module, section_name, §ion, |
| 125 | + (void **)&sec_buffer); |
| 126 | + if (ret < 0) { |
| 127 | + fprintf(stderr, |
| 128 | + "error: failed to read %s section content, code %d\n", |
| 129 | + section_name, ret); |
| 130 | + return ret; |
| 131 | + } |
| 132 | + |
| 133 | + temp_buff = malloc(*meta_section_size + section->size); |
| 134 | + if (!temp_buff) { |
| 135 | + free(sec_buffer); |
| 136 | + return -ENOMEM; |
| 137 | + } |
| 138 | + |
| 139 | + /* update element size and insert list after element header */ |
| 140 | + offset += head->elem_size; |
| 141 | + extra_size = ALIGN_UP(head->elem_size + section->size, EXT_MAN_ALIGN) - |
| 142 | + head->elem_size; |
| 143 | + head->elem_size += extra_size; |
| 144 | + memcpy(&temp_buff[0], msd_buff, offset); |
| 145 | + memcpy(&temp_buff[offset], sec_buffer, section->size); |
| 146 | + memcpy(&temp_buff[offset + section->size], &msd_buff[offset], |
| 147 | + *meta_section_size - offset); |
| 148 | + |
| 149 | + free(*meta_section_data); |
| 150 | + *meta_section_data = temp_buff; |
| 151 | + |
| 152 | + return 0; |
| 153 | +} |
| 154 | + |
84 | 155 | static int ext_man_build(const struct module *module, |
85 | 156 | struct ext_man_header **dst_buff) |
86 | 157 | { |
87 | 158 | struct ext_man_header ext_man; |
88 | 159 | const Elf32_Shdr *section; |
89 | 160 | uint8_t *buffer = NULL; |
90 | 161 | uint8_t *sec_buffer = NULL; |
91 | | - size_t offset; |
| 162 | + uint32_t sec_buffer_size; |
92 | 163 | int ret; |
93 | 164 |
|
94 | | - ret = elf_read_section(module, EXT_MAN_DATA_SECTION, §ion, |
95 | | - (void **)&sec_buffer); |
96 | | - if (ret < 0) { |
| 165 | + sec_buffer_size = elf_read_section(module, EXT_MAN_DATA_SECTION, |
| 166 | + §ion, (void **)&sec_buffer); |
| 167 | + if (sec_buffer_size < 0) { |
97 | 168 | fprintf(stderr, |
98 | 169 | "error: failed to read %s section content, code %d\n", |
99 | 170 | EXT_MAN_DATA_SECTION, ret); |
100 | | - goto out; |
| 171 | + return sec_buffer_size; |
| 172 | + } |
| 173 | + |
| 174 | + ret = ext_man_update_element_with_list(module, &sec_buffer_size, |
| 175 | + &sec_buffer, |
| 176 | + EXT_MAN_ELEM_UUID_DICT, |
| 177 | + EXT_MAN_UUID_DICT_SECTION); |
| 178 | + if (ret < 0) { |
| 179 | + fprintf(stderr, |
| 180 | + "error: failed to update extended manifest element with %s section content, code %d\n", |
| 181 | + EXT_MAN_UUID_DICT_SECTION, ret); |
| 182 | + goto err; |
101 | 183 | } |
102 | | - ret = 0; |
103 | 184 |
|
104 | 185 | /* fill ext_man struct, size aligned to 4 to avoid unaligned accesses */ |
105 | 186 | memcpy(&ext_man, &ext_man_template, sizeof(struct ext_man_header)); |
106 | 187 | ext_man.full_size = ext_man.header_size; |
107 | | - ext_man.full_size += section->size; |
| 188 | + ext_man.full_size += sec_buffer_size; |
108 | 189 | if (ext_man.full_size % 4) { |
109 | 190 | fprintf(stderr, |
110 | 191 | "error: extended manifest size must be aligned to 4\n"); |
111 | 192 | ret = -EINVAL; |
112 | | - goto out; |
| 193 | + goto err; |
113 | 194 | } |
114 | 195 |
|
115 | 196 | /* alloc buffer for ext_man */ |
116 | 197 | buffer = calloc(1, ext_man.full_size); |
117 | 198 | if (!buffer) { |
118 | 199 | ret = -ENOMEM; |
119 | | - goto out; |
| 200 | + goto err; |
120 | 201 | } |
121 | 202 |
|
122 | 203 | /* fill buffer with ext_man and section content */ |
123 | 204 | memcpy(buffer, &ext_man, ext_man.header_size); |
124 | | - offset = ext_man.header_size; |
125 | | - |
126 | | - memcpy(&buffer[offset],sec_buffer, section->size); |
| 205 | + memcpy(&buffer[ext_man.header_size], sec_buffer, sec_buffer_size); |
127 | 206 |
|
128 | 207 | *dst_buff = (struct ext_man_header *)buffer; |
129 | 208 |
|
130 | | -out: |
| 209 | +err: |
| 210 | + if (!sec_buffer) |
| 211 | + free(sec_buffer); |
131 | 212 | return ret; |
132 | 213 | } |
133 | 214 |
|
|
0 commit comments