QUESTION
UUID로 찾은 payload를 어디까지 읽을 수 있나?
fip_dev_init()는 플랫폼의 FIP image source를 얻어 backend 크기와 header를 확인한다. 이후 fip_file_open()은 ToC를 순서대로 읽어 요청 UUID와 처음 일치하는 entry를 찾고, 그 entry의 offset_address와 size를 backend 크기와 정수 한계에 대조한다.
성공 시 io_entity_t.info는 복사된 payload가 아니라 전역 current_fip_file을 가리킨다. fip_file_read()는 호출마다 backend를 열고 entry 상대 위치와 요청 길이를 검사한 뒤 정확히 읽었을 때만 cursor를 전진시킨다. 기본 MAX_FIP_DEVICES=1이며 전역 entry offset을 sentinel로 사용해 동시 open을 제한한다. offset 0인 entry도 범위 검사를 통과할 수 있으므로 절대적인 격리 보장으로 해석하지 않는다.
STRUCTURE
device별 크기와 전역 backend·file state
device slot · 최대 MAX_FIP_DEVICES개
dev_info_pool[i].info → state_pool[i]dev_spec, fip_size, platform ToC flag를 저장한다.fip_dev_init()plat_get_image_source() → backend open/size/header → backend close모든 device가 공유하는 단일 상태
backend_dev_handle + backend_image_specdevice별 배열이 아닌 전역 변수다.io_entity_t.info → current_fip_file선택한 entry, file_pos, FIP 크기를 보관한다. offset이 0이 아닌 열린 entry를 sentinel로 삼는다.여러 FIP device를 허용하는 빌드에서도 backend handle/spec와 열린 file state가 device별로 분리되지 않는다. offset 0 entry는 현재 sentinel 검사도 우회할 수 있다.
CALL PATH
초기화와 payload 접근은 서로 다른 backend open이다
① FIP device 초기화
plat_get_image_source(image_id,…)② 요청 UUID의 entity
entity->info는 항상 0으로 한다.여기서 io_seek()는 내부 backend handle에 대한 호출이다. FIP entity의 .seek callback은 NULL이다.
STATE LEDGER
성공 뒤에 유효한 값과 실패 시 결과
| 경계 | 검사·변경 | 다음 소비자 | 주요 실패 |
|---|---|---|---|
| device init | source 조회, io_size, 최소 header 길이, magic + nonzero serial, platform ToC flag | device의 fip_size와 전역 backend spec | source/open/유효하지 않은 header는 -ENOENT; FIP 크기가 header보다 작으면 -EINVAL; size 조회 실패·짧은 header read는 -EIO; backend read 자체의 오류는 원래 반환값 |
| file open 전제 | FIP 크기 ≥ header + entry, 전역 열린 파일 없음 | backend 재개방 | -EINVAL / -ENFILE |
| ToC scan | entry를 읽기 전 ToC 위치가 FIP 안인지 검사; 완전한 entry read; 첫 UUID 일치에서 중단 | current_fip_file.entry | ToC 범위 -EINVAL, 짧은 read -EIO, UUID 없음 -ENOENT |
| matched entry | size ≠ 0, offset/size/end가 FIP 및 SIZE_MAX 안인지 검사 | entity->info와 cursor 0 | 범위 위반 -EINVAL |
| read | cursor ≤ entry size, length ≤ remaining, backend offset/end ≤ FIP size, seek offset ≤ LLONG_MAX | 정확히 읽은 길이와 갱신 cursor | 범위 -EINVAL, 짧은 read -EIO |
| close | entry offset이 0이 아니면 전역 file state를 초기화; entity->info는 항상 0 | 다음 payload open 가능 | backend는 각 open/read에서 이미 닫힘 |
cleanup label로 진입한 실패에서 fip_file_open()은 전역 file state를 지운다. 최소 크기와 열린 파일 검사에서 직접 반환하면 이 정리를 실행하지 않는다. fip_file_read()는 성공한 정확한 read에서만 *length_read와 file_pos를 갱신한다.
VISUAL WALKTHROUGH
ToC 위치 검사와 payload 범위 검사는 다르다
toc_offset ≤ fip_size − sizeof(entry)offset_address + size ≤ fip_sizeio_size()가 보고한 FIP image 길이를 공통 상한으로 쓴다. scan은 UUID가 처음 일치하면 중단하므로, 모든 entry를 미리 검증하거나 중복 UUID가 없음을 증명하지 않는다. 일반적인 nonzero UUID를 찾다가 null UUID terminator에 닿으면 미발견으로 끝나며, terminator가 없어도 ToC 끝 밖을 읽기 전에 오류가 난다.
선택한 entry의 offset·size는 container 안에 있어야 하지만, 이 함수는 payload가 header/ToC와 겹치지 않는지 별도로 검사하지 않는다.
fip_file_read()의 상대 cursor에서 backend read까지file_pos ≤ entry.sizelength ≤ size − file_pos; 자동 절단 없음offset + file_pos; overflow·FIP 끝 검사bytes_read == length일 때만 cursor 갱신매 read가 backend를 열고 IO_SEEK_SET으로 절대 위치를 정한 뒤 닫는다. 짧은 read는 -EIO이며 부분 성공으로 cursor를 전진시키지 않는다.
UPSTREAM SOURCE
원본 코드
아래 코드는 Trusted Firmware-A LTS 2.14.3의 drivers/io/io_fip.c에서 50-138줄을 그대로 가져온 것이다.
50 uintptr_t dev_spec;
51 size_t fip_size;
52 uint16_t plat_toc_flag;
53} fip_dev_state_t;
54
55/*
56 * Only one file can be open across all FIP device
57 * as backends like io_memmap don't support
58 * multiple open files. The file state and
59 * backend handle should be maintained per FIP device
60 * if the same support is available in the backend
61 */
62static fip_file_state_t current_fip_file = {0};
63static uintptr_t backend_dev_handle;
64static uintptr_t backend_image_spec;
65
66static fip_dev_state_t state_pool[MAX_FIP_DEVICES];
67static io_dev_info_t dev_info_pool[MAX_FIP_DEVICES];
68
69/* Track number of allocated fip devices */
70static unsigned int fip_dev_count;
71
72/* Firmware Image Package driver functions */
73static int fip_dev_open(const uintptr_t dev_spec, io_dev_info_t **dev_info);
74static int fip_file_open(io_dev_info_t *dev_info, const uintptr_t spec,
75 io_entity_t *entity);
76static int fip_file_len(io_entity_t *entity, size_t *length);
77static int fip_file_read(io_entity_t *entity, uintptr_t buffer, size_t length,
78 size_t *length_read);
79static int fip_file_close(io_entity_t *entity);
80static int fip_dev_init(io_dev_info_t *dev_info, const uintptr_t init_params);
81static int fip_dev_close(io_dev_info_t *dev_info);
82
83
84/* Return 0 for equal uuids. */
85static inline int compare_uuids(const uuid_t *uuid1, const uuid_t *uuid2)
86{
87 return memcmp(uuid1, uuid2, sizeof(uuid_t));
88}
89
90
91static inline int is_valid_header(fip_toc_header_t *header)
92{
93 if ((header->name == TOC_HEADER_NAME) && (header->serial_number != 0)) {
94 return 1;
95 } else {
96 return 0;
97 }
98}
99
100
101/* Identify the device type as a virtual driver */
102static io_type_t device_type_fip(void)
103{
104 return IO_TYPE_FIRMWARE_IMAGE_PACKAGE;
105}
106
107
108static const io_dev_connector_t fip_dev_connector = {
109 .dev_open = fip_dev_open
110};
111
112
113static const io_dev_funcs_t fip_dev_funcs = {
114 .type = device_type_fip,
115 .open = fip_file_open,
116 .seek = NULL,
117 .size = fip_file_len,
118 .read = fip_file_read,
119 .write = NULL,
120 .close = fip_file_close,
121 .dev_init = fip_dev_init,
122 .dev_close = fip_dev_close,
123};
124
125/* Locate a file state in the pool, specified by address */
126static int find_first_fip_state(const uintptr_t dev_spec,
127 unsigned int *index_out)
128{
129 int result = -ENOENT;
130 unsigned int index;
131
132 for (index = 0; index < (unsigned int)MAX_FIP_DEVICES; ++index) {
133 /* dev_spec is used as identifier since it's unique */
134 if (state_pool[index].dev_spec == dev_spec) {
135 result = 0;
136 *index_out = index;
137 break;
138 }
READING CONTRACT
발췌에는 함수 본문이 없다
위 원본 코드 50–138행은 전역 상태, callback table, header 검사와 state 검색의 시작을 담는다. fip_file_open()의 74행은 선언이고 실제 함수 본문은 305–439행이다. fip_dev_init()은 218–290행, fip_file_read()는 454–535행을 읽어야 이 글의 범위·실패 설명을 검증할 수 있다.
RELATED SOURCE
함께 읽어야 하는 원본 코드
아래 두 발췌는 선언과 pool allocation 부근을 주로 반복한다. 실제 init/open/read 함수 본문은 링크한 고정 원문 218–553행에 있다.
01. FIP callback 선언과 device pool allocation의 앞부분
drivers/io/io_fip.c 67-169줄이다.
67static io_dev_info_t dev_info_pool[MAX_FIP_DEVICES];
68
69/* Track number of allocated fip devices */
70static unsigned int fip_dev_count;
71
72/* Firmware Image Package driver functions */
73static int fip_dev_open(const uintptr_t dev_spec, io_dev_info_t **dev_info);
74static int fip_file_open(io_dev_info_t *dev_info, const uintptr_t spec,
75 io_entity_t *entity);
76static int fip_file_len(io_entity_t *entity, size_t *length);
77static int fip_file_read(io_entity_t *entity, uintptr_t buffer, size_t length,
78 size_t *length_read);
79static int fip_file_close(io_entity_t *entity);
80static int fip_dev_init(io_dev_info_t *dev_info, const uintptr_t init_params);
81static int fip_dev_close(io_dev_info_t *dev_info);
82
83
84/* Return 0 for equal uuids. */
85static inline int compare_uuids(const uuid_t *uuid1, const uuid_t *uuid2)
86{
87 return memcmp(uuid1, uuid2, sizeof(uuid_t));
88}
89
90
91static inline int is_valid_header(fip_toc_header_t *header)
92{
93 if ((header->name == TOC_HEADER_NAME) && (header->serial_number != 0)) {
94 return 1;
95 } else {
96 return 0;
97 }
98}
99
100
101/* Identify the device type as a virtual driver */
102static io_type_t device_type_fip(void)
103{
104 return IO_TYPE_FIRMWARE_IMAGE_PACKAGE;
105}
106
107
108static const io_dev_connector_t fip_dev_connector = {
109 .dev_open = fip_dev_open
110};
111
112
113static const io_dev_funcs_t fip_dev_funcs = {
114 .type = device_type_fip,
115 .open = fip_file_open,
116 .seek = NULL,
117 .size = fip_file_len,
118 .read = fip_file_read,
119 .write = NULL,
120 .close = fip_file_close,
121 .dev_init = fip_dev_init,
122 .dev_close = fip_dev_close,
123};
124
125/* Locate a file state in the pool, specified by address */
126static int find_first_fip_state(const uintptr_t dev_spec,
127 unsigned int *index_out)
128{
129 int result = -ENOENT;
130 unsigned int index;
131
132 for (index = 0; index < (unsigned int)MAX_FIP_DEVICES; ++index) {
133 /* dev_spec is used as identifier since it's unique */
134 if (state_pool[index].dev_spec == dev_spec) {
135 result = 0;
136 *index_out = index;
137 break;
138 }
139 }
140 return result;
141}
142
143
144/* Allocate a device info from the pool and return a pointer to it */
145static int allocate_dev_info(io_dev_info_t **dev_info)
146{
147 int result = -ENOMEM;
148
149 assert(dev_info != NULL);
150
151 if (fip_dev_count < (unsigned int)MAX_FIP_DEVICES) {
152 unsigned int index = 0;
153
154 result = find_first_fip_state(0, &index);
155 assert(result == 0);
156 /* initialize dev_info */
157 dev_info_pool[index].funcs = &fip_dev_funcs;
158 dev_info_pool[index].info =
159 (uintptr_t)&state_pool[index];
160 *dev_info = &dev_info_pool[index];
161 ++fip_dev_count;
162 }
163
164 return result;
165}
166
167/* Release a device info to the pool */
168static int free_dev_info(io_dev_info_t *dev_info)
169{
02. callback 선언과 device pool allocation의 겹치는 발췌
drivers/io/io_fip.c 70-168줄이다.
70static unsigned int fip_dev_count;
71
72/* Firmware Image Package driver functions */
73static int fip_dev_open(const uintptr_t dev_spec, io_dev_info_t **dev_info);
74static int fip_file_open(io_dev_info_t *dev_info, const uintptr_t spec,
75 io_entity_t *entity);
76static int fip_file_len(io_entity_t *entity, size_t *length);
77static int fip_file_read(io_entity_t *entity, uintptr_t buffer, size_t length,
78 size_t *length_read);
79static int fip_file_close(io_entity_t *entity);
80static int fip_dev_init(io_dev_info_t *dev_info, const uintptr_t init_params);
81static int fip_dev_close(io_dev_info_t *dev_info);
82
83
84/* Return 0 for equal uuids. */
85static inline int compare_uuids(const uuid_t *uuid1, const uuid_t *uuid2)
86{
87 return memcmp(uuid1, uuid2, sizeof(uuid_t));
88}
89
90
91static inline int is_valid_header(fip_toc_header_t *header)
92{
93 if ((header->name == TOC_HEADER_NAME) && (header->serial_number != 0)) {
94 return 1;
95 } else {
96 return 0;
97 }
98}
99
100
101/* Identify the device type as a virtual driver */
102static io_type_t device_type_fip(void)
103{
104 return IO_TYPE_FIRMWARE_IMAGE_PACKAGE;
105}
106
107
108static const io_dev_connector_t fip_dev_connector = {
109 .dev_open = fip_dev_open
110};
111
112
113static const io_dev_funcs_t fip_dev_funcs = {
114 .type = device_type_fip,
115 .open = fip_file_open,
116 .seek = NULL,
117 .size = fip_file_len,
118 .read = fip_file_read,
119 .write = NULL,
120 .close = fip_file_close,
121 .dev_init = fip_dev_init,
122 .dev_close = fip_dev_close,
123};
124
125/* Locate a file state in the pool, specified by address */
126static int find_first_fip_state(const uintptr_t dev_spec,
127 unsigned int *index_out)
128{
129 int result = -ENOENT;
130 unsigned int index;
131
132 for (index = 0; index < (unsigned int)MAX_FIP_DEVICES; ++index) {
133 /* dev_spec is used as identifier since it's unique */
134 if (state_pool[index].dev_spec == dev_spec) {
135 result = 0;
136 *index_out = index;
137 break;
138 }
139 }
140 return result;
141}
142
143
144/* Allocate a device info from the pool and return a pointer to it */
145static int allocate_dev_info(io_dev_info_t **dev_info)
146{
147 int result = -ENOMEM;
148
149 assert(dev_info != NULL);
150
151 if (fip_dev_count < (unsigned int)MAX_FIP_DEVICES) {
152 unsigned int index = 0;
153
154 result = find_first_fip_state(0, &index);
155 assert(result == 0);
156 /* initialize dev_info */
157 dev_info_pool[index].funcs = &fip_dev_funcs;
158 dev_info_pool[index].info =
159 (uintptr_t)&state_pool[index];
160 *dev_info = &dev_info_pool[index];
161 ++fip_dev_count;
162 }
163
164 return result;
165}
166
167/* Release a device info to the pool */
168static int free_dev_info(io_dev_info_t *dev_info)
DETAILS
실제 구현의 세 경계
초기화는 header까지만 확인한다
fip_dev_init()는 플랫폼에서 image source를 얻고 backend 크기를 저장한다. header가 통째로 읽혔는지, name == TOC_HEADER_NAME과 nonzero serial인지 검사하고 platform ToC flag를 뽑는다. 이때 ToC 전체와 각 payload를 검증하지 않는다.
open은 첫 UUID 일치 entry를 선택한다
fip_file_open()은 ToC entry를 읽기 전에 남은 container 크기를 확인한다. entry를 전부 읽었을 때 UUID를 비교하며 일치하면 scan을 멈춘다. 찾은 entry의 크기·끝·정수 한계를 검사한 다음 current_fip_file.file_pos=0과 entity->info를 설정한다. 미발견이나 오류에서는 전역 file state를 지운다.
read는 요청량을 그대로 충족해야 한다
길이가 남은 payload보다 크면 -EINVAL이다. backend seek 뒤 io_read()가 정확히 요청 길이를 돌려주면 그때만 *length_read와 cursor가 갱신된다. FIP entity seek API는 없다. close는 entry offset이 0이 아닐 때 전역 state를 지우고 entity pointer는 항상 0으로 한다.
IMPLEMENTATION NOTES
범위 검사와 공유 상태의 한계
숫자 범위를 검사하되 ToC와의 겹침은 검사하지 않는다
open은 선택한 entry에 대해 zero size, container 경계, uint64 합산 wrap, SIZE_MAX를 검사한다. read는 상대 cursor·길이뿐 아니라 backend 절대 offset과 LLONG_MAX seek 인자도 검사한다. 이 조건은 container 내부의 숫자 범위를 제한하지만 payload와 metadata가 겹치지 않는다는 증명은 아니다.
단일 payload와 전역 backend spec
기본 MAX_FIP_DEVICES는 1이다. 더 크게 설정해도 current_fip_file, backend_dev_handle, backend_image_spec는 전역이다. 열린 entry의 offset_address != 0이면 다음 open은 -ENFILE을 반환한다. 여러 device가 독립된 열린 entity를 갖는다고 그리면 안 된다. 선택 entry의 offset 0은 별도로 금지하지 않으므로 sentinel과 정리 조건에 예외가 생긴다.
short read와 cleanup
header, ToC entry, payload를 요청 길이보다 적게 읽으면 각각 -EIO로 처리한다. backend를 연 뒤 cleanup 경로로 가는 open 실패는 backend를 닫고 전역 file state를 초기화한다. backend open 자체의 실패는 close 없이 state를 지우며, 그 앞의 크기·열린 파일 직접 반환은 state를 지우지 않는다. read 실패도 backend를 닫지만 cursor와 caller 길이는 성공 경로에서만 갱신한다. file_pos는 unsigned int이고 갱신은 file_pos += bytes_read이므로, 매우 큰 성공 read에서 cursor 폭을 넘지 않는다는 보장은 별도로 확인해야 한다.
DEEP ANALYSIS
조건별 반환값을 소스와 대조하기
| 조건 | 함수·위치 | 실제 결과 | 흔한 오해 |
|---|---|---|---|
| header가 짧게 읽힘 / magic 또는 serial 불일치 | fip_dev_init 264–283 | 각각 -EIO / -ENOENT | header flag 전체를 인증하지 않는다 |
| ToC의 다음 entry가 FIP 밖 | fip_file_open 365–370 | -EINVAL | terminator가 없다고 무한히 읽지 않는다 |
| UUID 미발견 / 선택 entry 범위 위반 | fip_file_open 396–429 | 각각 -ENOENT / -EINVAL | 모든 ToC entry를 선검증하지 않는다 |
| 요청 길이 > 남은 길이 | fip_file_read 481–492 | -EINVAL | EOF까지 자동 절단하지 않는다 |
| payload read가 짧음 | fip_file_read 514–528 | -EIO; cursor 유지 | 부분 read를 성공 처리하지 않는다 |
| FIP entity seek 요청 | callback table 113–123 | .seek = NULL | backend 내부 io_seek와 구분 |
이 표는 TF-A LTS 2.14.3 io_fip.c의 계약이다. 플랫폼 source policy와 backend 자체의 address/length 의미는 대상 플랫폼 구현에서 따로 확인해야 한다.
OBJECT LIFETIME
실제 저장 위치와 정리 시점
| 객체 | 설정·유효 범위 | 정리 또는 주의 |
|---|---|---|
fip_dev_state_t | device pool slot; dev_spec, fip_size, ToC flag | fip_dev_close가 slot을 지운다 |
| backend handle/spec | 플랫폼 source 조회가 설정하는 전역 변수 | backend I/O handle은 init/open/read마다 열고 닫는다; FIP device close는 전역 source를 지운다 |
fip_toc_header_t | init stack의 header buffer | 검사 후 flag만 state에 복사; buffer는 남지 않는다 |
current_fip_file | 전역 단일 file state; ToC entry 복사·cursor·FIP 크기 | open의 cleanup 경로 실패 시 초기화; 초기 -EINVAL/-ENFILE 직접 반환은 유지. entity close는 entry offset이 0이 아닐 때만 초기화 |
io_entity_t.info | 선택된 전역 file state의 pointer | entity close에서 0으로 설정; 독립 payload 사본 아님 |
FAILURE PATH
관찰할 오류와 첫 검사 위치
| 증상 | 처음 확인할 조건 | 기준 코드 |
|---|---|---|
| FIP 초기화 거부 | source/open, io_size, header 길이·magic·serial | 218–290 |
UUID 조회 중 -EINVAL | ToC entry 읽기 전 범위 또는 선택 entry의 offset/size/end | 329–332, 365–370, 396–416 |
UUID 조회 중 -ENOENT | backend open/seek 실패와 실제 UUID 미발견을 구분 | 345–360, 425–429 |
두 번째 payload open이 -ENFILE | 현재 열린 전역 entry의 offset이 0이 아닌지 | 334–343 |
read -EINVAL / -EIO | cursor·remaining·absolute offset / 짧은 backend read | 481–503, 514–528 |
실패 반환 뒤의 *length_read 값을 새 결과로 간주하지 않는다. 이 함수는 정확히 성공한 read에서만 값을 기록한다.
EVIDENCE
고정 원문과 실제 빌드에서 확인할 값
이 리뷰는 TF-A를 빌드하거나 보드에서 실행하지 않았다. 대상 플랫폼을 확인할 때는 아래 항목을 순서대로 대조한다.
| 자료 | 확인 항목 | 판정 |
|---|---|---|
| LTS 2.14.3 원문 218–553 | init, ToC scan, read와 close의 분기 | 정확한 반환값과 cleanup |
플랫폼 plat_get_image_source() | FIP_IMAGE_ID가 가리키는 backend device/spec | FIP 크기·offset의 기준 |
fiptool info <fip.bin> | 요청 UUID에 대응하는 payload와 크기 | FIP 내용의 정적 대조; 드라이버 동작 증명은 아님 |
| 해당 stage의 ELF·허용된 debug log | fip_file_open, fip_file_read 및 cursor/offset/length | 그 플랫폼에 드라이버가 실제 포함·호출되는지 |
LAB
실험할 때 나눠야 할 세 실패
- 01terminator 없는 ToC
찾지 못하는 UUID로 scan한다. 다음 entry가 container 밖으로 나가기 전에
-EINVAL이 나는지 확인한다. - 02선택 entry의 끝이 container 밖
UUID는 맞되
offset + size가 FIP 크기를 넘는 entry를 사용한다. open이-EINVAL을 돌려주고 entity가 공개되지 않는지 확인한다. - 03payload short read
backend가 요청보다 적은 byte를 돌려줄 때
-EIO, cursor 불변, backend close를 대조한다. 정상 부팅 매체 대신 격리된 시험 입력을 사용한다.
PRIMARY REFERENCES