QUESTION
boot_linux_from_mmc()는 어떤 image를 어디서 읽고 언제 kernel에 넘기는가?
고정 소스는 먼저 EMMC_BOOT_IMG_HEADER_ADDR의 magic을 본다. 맞으면 goto unified_boot로 partition/header/payload read를 건너뛰고 그 header의 주소로 boot_linux()에 간다. 일반 경로만 boot 또는 recovery partition을 고르고 header page를 읽는다.
일반 경로의 header magic은 비교하지만, 0이 아닌 hdr->page_size는 여기서 허용 범위 검사 없이 page_size와 mask에 반영한다. 이후 signed·locked·untampered 조건에서 scratch와 signature page를 읽는 분기, 그렇지 않으면 kernel·ramdisk를 목적지에 직접 읽는 분기로 갈린다. 이 함수의 성공 흐름을 모든 component의 partition/RAM bounds 검증으로 읽으면 안 된다.
classic AOSP aboot commit 9d6e28f의 eMMC 함수다. NAND/flash용 boot_linux_from_flash() 및 현대 Android A/B·AVB·GKI 정책과 구분한다.
고정 aboot.c:506–786에서 각 조건을 직접 확인할 수 있다.
STRUCTURE
두 header 출처와 두 payload 적재 방식
boot/recovery partition header01의 magic이 맞으면 02–04를 건너뛰고 unified_boot로 간다. 03과 04는 일반 경로에서 서로 다른 대안이다.
블록의 배치는 RAM 주소 순서가 아니다. 원문은 header의 kernel·ramdisk·tags 주소를 조건부로 보정한 뒤 VA로 변환하고, 각 적재 분기를 실행한다.
CALL PATH
unified 우회 뒤의 일반 partition 경로
boot_linux_from_mmc()의 결정 경계boot/recovery header read·magic 검사boot_linux()unified branch는 곧바로 unified_boot label로 간다. 일반 branch는 signed scratch와 direct read 중 하나만 실행하고, 해당 branch의 DT 처리 뒤 boot_linux()로 합류한다. 두 경로가 같은 검사를 통과했다는 뜻은 아니다.
STATE LEDGER
분기별 입력과 실제 검사 범위
aboot_init()의 eMMC 초기 page_size=2048에서 시작한다. 좁은 화면에서는 좌우로 밀어 분기 조건을 확인한다.
| # | 조건·실행 | 코드에서 확인되는 상태 | 다음 동작 |
|---|---|---|---|
| 01 | 고정 주소 uhdr magic 일치 | preloaded unified header를 사용; partition read·주소 보정 생략 | unified_boot → boot_linux() |
| 02 | 그 외 boot/recovery 선택 | partition offset이 0이면 오류; header mmc_read() 후 magic 비교 | magic 불일치면 -1 반환 |
| 03 | hdr->page_size가 0이 아니고 다름 | 그 값을 page_size·page_mask에 반영; 여기에는 허용 범위 검사 없음 | 기본 주소 조건부 보정·VA 변환 |
| 04 | signed 사용 ∧ locked ∧ not tampered | scratch에 image, 뒤따르는 signature page를 읽고 성공적으로 읽으면 image_verify() | 검증 실패·signature read 실패도 tampered 상태로 복사·부팅 진행 |
| 05 | 그 밖의 일반 경로 | rounded kernel·ramdisk를 header 목적지로 각각 mmc_read() | second_size != 0이면 ASSERT(0) |
| 06 | DEVICE_TREE 빌드 | dt_size 있으면 table/entry에서 선택; 없으면 appended DTB 시도 | signed는 scratch, direct는 eMMC에서 DT 내용을 가져옴 |
| 07 | unified_boot label | 현재 hdr의 kernel·tags·ramdisk 주소와 cmdline 전달 | boot_linux()가 DT/ATAG 구성 후 kernel 진입 |
이 함수는 partition 크기와 각 destination RAM 범위를 포괄적으로 검사하는 단계를 갖지 않는다. signed branch의 검증 실패도 즉시 반환하는 조건이 아니다.
VISUAL WALKTHROUGH
page offset과 실행 분기를 따로 읽기
그림 3은 일반 경로에서 쓰는 논리적 file offset이다. image가 메모리의 어느 높이로 적재되는지는 표시하지 않는다. 그림 4는 같은 header를 사용하는 두 적재 방식의 차이다.
ROUND_TO_PAGE(size,page_mask)를 사용하며, second stage는 정상적인 로드 대상이 아니다.
page_size; rounded readASSERT(0)DEVICE_TREE·dt_size 조건부unified header가 발견되면 두 열 모두 건너뛴다.
signed ∧ locked ∧ not tampered
읽기scratch에 image, 뒤에 signature page
인증signature read 성공 시 image_verify(); 실패해도 즉시 return 없음
배치kernel·ramdisk·조건부 DT를 memmove()
상태검증 성공 때만 is_tampered=0
그 밖의 일반 경로
읽기kernel → ramdisk를 목적지에 mmc_read()
secondsecond_size != 0이면 ASSERT(0)
DTDEVICE_TREE && dt_size면 table·선택 entry read
그 외DEVICE_TREE에서 dt_size=0이면 appended DTB 시도
UPSTREAM SOURCE
원본 코드
아래 코드는 LK / classic AOSP aboot AOSP 9d6e28f의 app/aboot/aboot.c에서 482-570줄을 그대로 가져온 것이다.
482 enter_critical_section();
483
484 /* do any platform specific cleanup before kernel entry */
485 platform_uninit();
486
487 arch_disable_cache(UCACHE);
488
489#if ARM_WITH_MMU
490 arch_disable_mmu();
491#endif
492 bs_set_timestamp(BS_KERNEL_ENTRY);
493 entry(0, machtype, (unsigned*)tags_phys);
494}
495
496unsigned page_size = 0;
497unsigned page_mask = 0;
498
499#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
500
501static unsigned char buf[4096]; //Equal to max-supported pagesize
502#if DEVICE_TREE
503static unsigned char dt_buf[4096];
504#endif
505
506int boot_linux_from_mmc(void)
507{
508 struct boot_img_hdr *hdr = (void*) buf;
509 struct boot_img_hdr *uhdr;
510 unsigned offset = 0;
511 unsigned long long ptn = 0;
512 int index = INVALID_PTN;
513
514 unsigned char *image_addr = 0;
515 unsigned kernel_actual;
516 unsigned ramdisk_actual;
517 unsigned imagesize_actual;
518 unsigned second_actual = 0;
519
520#if DEVICE_TREE
521 struct dt_table *table;
522 struct dt_entry *dt_entry_ptr;
523 unsigned dt_table_offset;
524 uint32_t dt_actual;
525#endif
526
527 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
528 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
529 dprintf(INFO, "Unified boot method!\n");
530 hdr = uhdr;
531 goto unified_boot;
532 }
533 if (!boot_into_recovery) {
534 index = partition_get_index("boot");
535 ptn = partition_get_offset(index);
536 if(ptn == 0) {
537 dprintf(CRITICAL, "ERROR: No boot partition found\n");
538 return -1;
539 }
540 }
541 else {
542 index = partition_get_index("recovery");
543 ptn = partition_get_offset(index);
544 if(ptn == 0) {
545 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
546 return -1;
547 }
548 }
549
550 if (mmc_read(ptn + offset, (unsigned int *) buf, page_size)) {
551 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
552 return -1;
553 }
554
555 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
556 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
557 return -1;
558 }
559
560 if (hdr->page_size && (hdr->page_size != page_size)) {
561 page_size = hdr->page_size;
562 page_mask = page_size - 1;
563 }
564
565 /*
566 * Update the kernel/ramdisk/tags address if the boot image header
567 * has default values, these default values come from mkbootimg when
568 * the boot image is flashed using fastboot flash:raw
569 */
570 update_ker_tags_rdisk_addr(hdr);
READING CONTRACT
발췌 범위와 읽기 기준
원문 482–570줄에는 boot_linux() 끝과 boot_linux_from_mmc()의 초반이 함께 있다. 함수 시작은 L506이다. 발췌만으로 signed·direct 적재와 DT 처리를 판단하지 말고 같은 고정 source의 L581–786을 이어 읽는다.
특히 unified header는 L528–532에서 일반 partition read를 우회한다. page size 갱신은 검사 완료 표식이 아니고, second_size의 처리도 적재 분기에 따라 다르다.
RELATED SOURCE
함께 읽어야 하는 원본 코드
아래 발췌는 기본 주소를 조건부로 바꾸는 update_ker_tags_rdisk_addr()의 문맥이다. signed scratch·signature 경로와 직접 kernel/ramdisk read·second·DT 경로는 같은 고정 파일에서 이어서 확인한다.
01. boot image header의 kernel, tags, ramdisk 주소 보정
app/aboot/aboot.c 148-230줄이다.
148
149char max_download_size[MAX_RSP_SIZE];
150char sn_buf[13];
151
152extern int emmc_recovery_init(void);
153
154#if NO_KEYPAD_DRIVER
155extern int fastboot_trigger(void);
156#endif
157
158static void update_ker_tags_rdisk_addr(struct boot_img_hdr *hdr)
159{
160 /* overwrite the destination of specified for the project */
161 /*
162 * Update the value to sane values only when the boot image
163 * header does not have sane values, this is added to make sure
164 * that we always use values from boot.img header and use the
165 * force values when boot image header has default values.
166 */
167#ifdef ABOOT_FORCE_KERNEL_ADDR
168 if (hdr->kernel_addr == ABOOT_DEFAULT_KERNEL_ADDR)
169 hdr->kernel_addr = ABOOT_FORCE_KERNEL_ADDR;
170#endif
171#ifdef ABOOT_FORCE_RAMDISK_ADDR
172 if (hdr->ramdisk_addr == ABOOT_DEFAULT_RAMDISK_ADDR)
173 hdr->ramdisk_addr = ABOOT_FORCE_RAMDISK_ADDR;
174#endif
175#ifdef ABOOT_FORCE_TAGS_ADDR
176 if (hdr->tags_addr == ABOOT_DEFAULT_TAGS_ADDR)
177 hdr->tags_addr = ABOOT_FORCE_TAGS_ADDR;
178#endif
179}
180
181static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
182{
183 struct atag_ptbl_entry atag_ptn;
184
185 memcpy(atag_ptn.name, ptn->name, 16);
186 atag_ptn.name[15] = '\0';
187 atag_ptn.offset = ptn->start;
188 atag_ptn.size = ptn->length;
189 atag_ptn.flags = ptn->flags;
190 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
191 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
192}
193
194unsigned char *update_cmdline(const char * cmdline)
195{
196 int cmdline_len = 0;
197 int have_cmdline = 0;
198 unsigned char *cmdline_final = NULL;
199 int pause_at_bootup = 0;
200
201 if (cmdline && cmdline[0]) {
202 cmdline_len = strlen(cmdline);
203 have_cmdline = 1;
204 }
205 if (target_is_emmc_boot()) {
206 cmdline_len += strlen(emmc_cmdline);
207 }
208
209 cmdline_len += strlen(usb_sn_cmdline);
210 cmdline_len += strlen(sn_buf);
211
212 if (target_pause_for_battery_charge()) {
213 pause_at_bootup = 1;
214 cmdline_len += strlen(battchg_pause);
215 }
216
217 if(target_use_signed_kernel() && auth_kernel_img) {
218 cmdline_len += strlen(auth_kernel);
219 }
220
221 /* Determine correct androidboot.baseband to use */
222 switch(target_baseband())
223 {
224 case BASEBAND_APQ:
225 cmdline_len += strlen(baseband_apq);
226 break;
227
228 case BASEBAND_MSM:
229 cmdline_len += strlen(baseband_msm);
230 break;
DETAILS
일반 eMMC 경로의 조건을 순서대로 읽기
unified header는 partition 로드를 우회한다
EMMC_BOOT_IMG_HEADER_ADDR에서 BOOT_MAGIC이 맞으면 hdr = uhdr와 goto unified_boot를 수행한다. 따라서 이 branch에 일반 boot/recovery partition read·address update·signature check를 적용해 설명하지 않는다.
일반 header의 page size는 채택이지 범위 검사가 아니다
partition offset이 0인지 검사하고 첫 page_size만큼 buf에 읽은 뒤 magic을 비교한다. header의 page_size가 0이 아니고 기존 값과 다르면 그대로 page_size/page_mask에 넣는다. 함수 안에는 이 새 값의 허용 범위, partition 총 길이 또는 목적지 전체 범위 검사 단계가 없다.
주소 보정은 기본 값일 때만 조건부다
update_ker_tags_rdisk_addr()는 빌드의 ABOOT_FORCE_*_ADDR가 있고 header 주소가 대응하는 default일 때만 kernel·ramdisk·tags 주소를 바꾼다. 이어 VA()로 header 주소를 가상 주소로 변환한다. 이 동작이 RAM 충돌 여부를 확인한다는 뜻은 아니다.
signed branch의 인증 실패는 부팅 중단이 아니다
target_use_signed_kernel()·locked·not tampered일 때 scratch에 image를 읽고 signature page read가 성공하면 image_verify()를 호출한다. 먼저 is_tampered=1로 두고 verify 성공일 때만 0으로 돌린다. signature read/verify 실패 뒤에도 kernel·ramdisk·조건부 DT 복사와 boot_linux()로 흐를 수 있다.
직접 경로에서 second와 DT는 별도 조건이다
다른 상태에서는 rounded kernel·ramdisk를 목적지에 읽는다. second_size != 0이면 second를 적재하지 않고 ASSERT(0)이다. DEVICE_TREE 빌드에서만 dt_size가 있는 table entry를 읽거나, 없으면 appended DTB를 찾는다. 마지막 boot_linux()는 DT를 업데이트하거나 ATAG를 만든 뒤 kernel entry를 호출한다.
IMPLEMENTATION NOTES
계산식과 검증의 한계
rounded size와 실제 복사 size는 다르다
직접 경로의 mmc_read() 길이는 rounded kernel_actual/ramdisk_actual이고 다음 offset도 rounded 값으로 올린다. signed scratch 경로는 image를 한 번 읽은 뒤 memmove()에 header의 원래 kernel_size/ramdisk_size를 쓴다. 두 방식을 하나의 component read 순서로 합치면 안 된다.
second_size는 지원된 stage로 묘사할 수 없다
직접 branch는 second_actual을 계산하지만 second_size != 0이면 ASSERT(0)으로 멈춘다. signed scratch branch의 imagesize_actual은 second size를 더하지 않는다. 명목상 format field가 있다는 사실과 이 함수가 second를 로드한다는 주장은 다르다.
DT table 검사는 완전한 bounds 보장이 아니다
DEVICE_TREE와 dt_size 조건에서 table page와 선택한 entry를 사용한다. header의 magic·version 조건은 둘 다 다를 때만 오류이며, entry table 크기 ASSERT는 header page를 기준으로 한다. 이 코드만으로 selected entry의 모든 storage·destination bounds가 검증됐다고 말할 수 없다.
OBJECT LIFETIME
branch마다 달라지는 객체와 주소
| 대상 | 출처 | 사용 경로 | 확인할 조건 |
|---|---|---|---|
uhdr | EMMC_BOOT_IMG_HEADER_ADDR | magic 일치 시 unified_boot | partition/header/payload read 우회; 이미 놓인 주소 사용 |
buf[4096] / hdr | 일반 경로의 header mmc_read() | magic·page_size·component fields | header page 크기와 고정 buffer의 관계를 별도 검토 |
image_addr scratch | target_get_scratch_address() | signed·locked·not tampered branch | image+signature page 저장 뒤 header 목적지로 memmove() |
hdr->kernel_addr/ramdisk_addr | header, 조건부 force, VA() | direct read 또는 scratch copy | source에 목적지 범위·상호 충돌의 일반 검사 없음 |
dt_buf[4096]/tags_addr | DEVICE_TREE 빌드의 table·entry | signed/direct의 조건부 DT와 boot_linux() | dt_size=0이면 appended DTB 탐색 |
FAILURE PATH
반환·중단·계속 진행을 나누기
| # | 조건 | 고정 코드의 반응 | 확인할 범위 |
|---|---|---|---|
| 01 | partition offset 0, header read 실패, magic 불일치 | -1 반환 | 일반 header 경로 |
| 02 | signed branch의 image read 실패 | -1 반환 | scratch read |
| 03 | signature read 실패 또는 image_verify() 실패 | tampered 상태로 copy·boot 진행 가능 | signed branch의 인증 상태 |
| 04 | direct branch에서 second_size != 0 | ASSERT(0); second load 미구현 | ramdisk read 직후 |
| 05 | DT table/entry read·선택 실패 | 일부는 -1 반환, 일부는 ASSERT | DEVICE_TREE && dt_size |
header page size가 허용 범위를 넘거나 destination이 겹치는 경우를 이 함수가 일반적으로 걸러 준다고 가정하지 않는다.
EVIDENCE
그림의 각 branch를 검증할 고정 소스
같은 commit의 정확한 위치를 열어 동작과 주장 범위를 대조한다. 제품 보드의 partition/RAM 보호 수준은 별도 구현 증거가 필요하다.
| # | 질문 | 고정 원문 | 확인할 조건 |
|---|---|---|---|
| 01 | 초기 page size | aboot.c:1802–1812 | eMMC이면 2048, 그 밖은 flash page size |
| 02 | unified와 header | aboot.c:526–579 | magic 우회, partition 선택, header page read·조건부 page size |
| 03 | signed scratch | aboot.c:581–692 | signature read·verify 결과·복사·tamper 상태 |
| 04 | direct read와 second | aboot.c:693–731 | rounded read, second_size ASSERT |
| 05 | DT와 kernel entry | aboot.c:732–786 | DT table/appended 조건, boot_linux() |
LAB
조건을 분리해 확인할 실험
- 01unified와 일반 header 비교
읽기 전용 소스 계측 또는 복구 가능한 시험 환경에서 고정 주소 magic 유무를 바꿔 partition
mmc_read()호출 여부를 기록한다. unified 경로에는 일반 header 검사 결과를 대입하지 않는다. - 02signed 실패와 direct 경로 분리
target_use_signed_kernel(), unlock, tamper 상태를 조합해 scratch/image/signature read와 직접 kernel/ramdisk read를 각각 추적한다. signature read 실패가 즉시 boot 중단인지도 별도로 확인한다. - 03second·DT 조건의 source-only 검토
second_size,DEVICE_TREE,dt_size조합에서ASSERT(0), DT table read, appended DTB 호출 위치를 고정 코드로 표로 만든다. 잘못된 image를 실장치에서 실행시키지 않는다.
PRIMARY REFERENCES