U-Boot · source analysis

DRAM 상단 예약과 relocation 주소

DRAM 상단에서 U-Boot, malloc, FDT, stack 영역을 차감해 relocation 목적지를 정하는 계산을 분석합니다.

표의 버전은 소스 분석 기준이며 최신 보안 버전 권고가 아닙니다. 제품 적용 시 사용 플랫폼의 errata를 확인하세요.

Series
05 / 22
Baseline
v2026.07
Commit
ece349ade297
Source
common/board_f.c:681-769 (발췌 범위)

예약 주소는 언제 실제 U-Boot 이미지가 되는가?

board_init_f()는 DRAM 상단에서 주소를 계획한다. reserve_uboot()가 gd->relocaddr를 정한 순간은 이미지가 복사된 순간이 아니다. setup_reloc()에서도 전체 이미지는 복사하지 않는다.

이 글은 주소 예약 → GD 준비 → ARM64 이미지 복사를 구분한다. 복사 경로 설명은 U-Boot proper의 ARM64 기준이며 XPL과 다른 architecture는 별도 경로다.

두 주소 cursor와 두 복사의 경계

그림 1. 계획 단계의 gd_t 복사와 ARM64 U-Boot 이미지 복사는 다르다

initcall 배열 · ARM64 crt0 · 이미지 copy loop

고정 소스에서 실제로 이어지는 순서

순서함수·경로바뀌는 상태이미지 복사?
01dram_init() → setup_dest_addr()ram_top, relocaddr 초기화아니요
02PRAM · bloblist · MMU · video · trace (구성별)상단 주소 선점·정렬아니요
03reserve_uboot()!GD_FLG_SKIP_RELOC && !SKIP_RELOCATE_CODE일 때 mon_len 차감·정렬; 이후 start_addr_sp 설정아니요
04reserve_malloc() 이하start_addr_sp 하향, new_gd 등 기록아니요
05setup_reloc()reloc_off 계산, gd_t 복사GD만 복사
06ARM64 proper: crt0_64.S → relocate_code()이미지 copy·fixup, BSS clear, board_init_r()조건부

기존 그림의 reserve_top은 이 고정 버전의 단일 함수가 아니다. X86/ARC와 기타 architecture의 전환도 ARM64와 다르다.

주소값의 의미가 바뀌는 시점

값설정 지점의미잘못 읽기 쉬운 점
ram_topsetup_dest_addr()보드가 사용할 수 있는 DRAM 상단물리 DRAM 마지막 주소와 같다고 단정 금지
relocaddrreserve_uboot()까지의 상단 cursorU-Boot 이미지 목적지복사 완료 flag가 아님
start_addr_spreserve_uboot()에서 시작, runtime 예약에서 감소malloc·GD·FDT·stack 아래쪽 주소relocaddr와 계속 같은 값이 아님
new_gdreserve_global_data()새 GD 목적지; setup_reloc()에서 구조체 복사전체 이미지 사본이 아님
reloc_offsetup_reloc()목적지와 링크/이미지 기준의 차이copy loop나 fixup 자체가 아님

높은 주소에서 낮은 주소로 내려가는 배치

아래 표는 상대적 순서다. PRAM, MMU, video, trace, FDT, bootstage, bloblist와 arch 예약은 설정에 따라 달라진다. Linux kernel Image·initrd·DTB의 booti 재배치는 U-Boot 자신의 relocation과 별개다.

주소 위치함수실제로 하는 일
usable RAM topsetup_dest_addr()relocaddr의 출발점
선택적 상단 영역reserve_pram(), bloblist, MMU, video, trace선점과 정렬; 보드·구성별
U-Boot 이미지 목적지reserve_uboot()skip 조건이 없을 때 mon_len 차감·4 KiB 정렬; 아직 복사하지 않음
runtime 영역reserve_malloc() 이하start_addr_sp를 낮춰 malloc·metadata·stack 공간 계획

검산할 때 구간을 [start,end)로 표시하고 정렬로 실제 소비한 바이트와 실제 RAM 범위를 비교한다. reserve_uboot() 뒤의 모든 예약이 relocaddr를 계속 낮추는 것은 아니다.

원본 코드

아래 코드는 U-Boot v2026.07의 common/board_f.c에서 681-769줄을 그대로 가져온 것이다.

commitece349ade2973e220f524ce59e59711cc919263fanchor · L705static int setup_reloc(void)
681	/*
682	 * Relocate only if we are supposed to send it
683	 */
684	if ((gd->flags & GD_FLG_SKIP_RELOC) &&
685	    CONFIG_BLOBLIST_SIZE == CONFIG_BLOBLIST_SIZE_RELOC) {
686		debug("Not relocating bloblist\n");
687		return 0;
688	}
689	if (gd->boardf->new_bloblist) {
690		ulong size = bloblist_get_total_size();
691
692		if (size < CONFIG_BLOBLIST_SIZE_RELOC)
693			size = CONFIG_BLOBLIST_SIZE_RELOC;
694
695		debug("Copying bloblist from %p to %p, size %lx\n",
696		      gd->bloblist, gd->boardf->new_bloblist, size);
697		return bloblist_reloc(gd->boardf->new_bloblist, size);
698	}
699#endif
700
701	return 0;
702}
703
704void mcheck_on_ramrelocation(size_t offset);
705static int setup_reloc(void)
706{
707	if (!(gd->flags & GD_FLG_SKIP_RELOC)) {
708#ifdef CONFIG_TEXT_BASE
709#ifdef ARM
710		gd->reloc_off = gd->relocaddr - (unsigned long)__image_copy_start;
711#elif defined(CONFIG_MICROBLAZE)
712		gd->reloc_off = gd->relocaddr - (u32)_start;
713#elif defined(CONFIG_M68K)
714		/*
715		 * On all ColdFire arch cpu, monitor code starts always
716		 * just after the default vector table location, so at 0x400
717		 */
718		gd->reloc_off = gd->relocaddr - (CONFIG_TEXT_BASE + 0x400);
719#elif !defined(CONFIG_SANDBOX)
720		gd->reloc_off = gd->relocaddr - CONFIG_TEXT_BASE;
721#endif
722#endif
723	}
724
725	memcpy(gd->new_gd, (char *)gd, sizeof(gd_t));
726
727	if (gd->flags & GD_FLG_SKIP_RELOC) {
728		debug("Skipping relocation due to flag\n");
729	} else {
730#ifdef MCHECK_HEAP_PROTECTION
731		mcheck_on_ramrelocation(gd->reloc_off);
732#endif
733		debug("Relocation Offset is: %08lx\n", gd->reloc_off);
734		debug("Relocating to %08lx, new gd at %08lx, sp at %08lx\n",
735		      gd->relocaddr, (ulong)map_to_sysmem(gd->new_gd),
736		      gd->start_addr_sp);
737	}
738
739	return 0;
740}
741
742#if CONFIG_IS_ENABLED(OF_BOARD_FIXUP)
743static int fix_fdt(void)
744{
745	return board_fix_fdt((void *)gd->fdt_blob);
746}
747#endif
748
749/* ARM calls relocate_code from its crt0.S */
750#if !defined(CONFIG_ARM) && !defined(CONFIG_SANDBOX)
751
752static int jump_to_copy(void)
753{
754	if (gd->flags & GD_FLG_SKIP_RELOC)
755		return 0;
756	/*
757	 * x86 is special, but in a nice way. It uses a trampoline which
758	 * enables the dcache if possible.
759	 *
760	 * For now, other archs use relocate_code(), which is implemented
761	 * similarly for all archs. When we do generic relocation, hopefully
762	 * we can make all archs enable the dcache prior to relocation.
763	 */
764#if defined(CONFIG_X86) || defined(CONFIG_ARC)
765	/*
766	 * SDRAM and console are now initialised. The final stack can now
767	 * be setup in SDRAM. Code execution will continue in Flash, but
768	 * with the stack in SDRAM and Global Data in temporary memory
769	 * (CPU cache)

발췌 89행과 빠진 전환 코드

원본 발췌 681–769행은 고정 commit ece349ade2973e220f524ce59e59711cc919263f의 common/board_f.c와 행별로 대조했다. setup_reloc()는 705행부터, 725행 memcpy의 크기는 sizeof(gd_t)다. 전체 U-Boot 이미지 copy loop는 이 발췌에 없다.

주소 시작, 이미지 목적지 예약, runtime 예약, initcall 순서를 함께 읽는다. ARM64의 실제 복사·전환은 crt0 및 relocate_64.S에 있다.

예약과 이미지 전환을 분리해 읽기

01

usable RAM top을 고른다

setup_dest_addr()는 메모리 크기와 board_get_usable_ram_top()을 거쳐 ram_top 및 최초 relocaddr를 정한다. 보고된 총 DRAM 끝을 무조건 쓰지 않는다.

02

두 cursor의 전환점

reserve_uboot()는 skip 조건이 없을 때 mon_len을 빼고 정렬한 뒤 start_addr_sp = relocaddr로 둔다. 이후 malloc, board info, GD, FDT, stack은 주로 start_addr_sp를 낮춘다.

03

GD 복사와 이미지 복사는 다르다

setup_reloc()가 복사하는 것은 gd_t다. ARM64 U-Boot proper에서는 board_init_f() 반환 뒤 crt0_64.S가 새 SP/GD를 사용하고 relocate_code()가 이미지와 상대 fixup을 처리한다.

04

건너뛰는 조건

reserve_uboot()의 mon_len 차감은 GD_FLG_SKIP_RELOC가 없고 CONFIG_SKIP_RELOCATE_CODE도 꺼졌을 때만 한다. CONFIG_SKIP_RELOCATE가 세우는 flag는 ARM64 crt0의 이미지 복사 분기에서 읽는다. relocate_64.S는 소스와 목적지가 같아도 복사 루프를 건너뛴다. XPL은 별도 경로다.

주소 오류와 복사 오류를 가르는 표

질문소스 관찰 지점판정
이미지 목적지가 유효한가?board_get_usable_ram_top(), reserve_uboot()정렬 뒤 구간이 실제 사용 가능 DRAM과 겹치는가
runtime 영역은 어디까지 내려가나?reserve_stack_aligned() 및 후속 reservestart_addr_sp와 이미지 목적지를 분리해 추적
무엇이 복사됐나?setup_reloc()의 sizeof(gd_t) 대 ARM64 copy_loopGD 구조체 사본과 이미지 사본 구별
새 코드로 언제 진입하나?crt0_64.S의 relocate return과 board_init_r()예약 직후에는 옛 PC로 실행 중임

copy loop에서 fault가 나도 원인은 앞서 고른 목적지가 비존재 DRAM이거나 다른 펌웨어 영역을 덮는 것일 수 있다. 보드의 secure carveout은 보드별 자료로 확인한다. FDT reserved-memory 선언이 자동으로 모든 초기 U-Boot 주소 계산에 반영된다고 단정하지 않는다.

예약된 공간과 실제 내용

대상주소 예약 / 쓰기뒤에서 읽는 쪽
U-Boot 목적지 relocaddrreserve_uboot()가 위치를 정함ARM64 relocate_code()의 x0
새 GD new_gdreserve_global_data()가 공간 예약; setup_reloc()가 내용 복사ARM64 crt0의 x18와 board_init_r()
새 stack start_addr_spruntime reserve와 reserve_stacks()에서 확정ARM64 crt0에서 SP 설정
U-Boot 이미지 사본ARM64 proper의 relocate_code() copy loop와 fixuprelocation return 뒤의 코드

증상과 원인 위치를 분리

관측먼저 볼 값가능한 경계
복사 시작에서 faultram_top, relocaddr, mon_len, 실제 DRAM map목적지가 유효 RAM 밖이거나 carveout과 겹침
새 GD 또는 stack 진입 뒤 실패new_gd, start_addr_sp, malloc/FDT/stack 구간runtime 예약 겹침 또는 정렬 오류
이미지 복사가 보이지 않음GD_FLG_SKIP_RELOC, XPL, 소스=목적지의도된 skip branch 가능성
복사 뒤에만 이상reloc_off, .rela.dyn, BSS, cache 조건복사와 fixup/전환 경계

빌드와 보드에서 남길 증거

관찰기록할 값
빌드 설정과 mapCONFIG_SKIP_RELOCATE*, mon_len, __image_copy_start/end
${CROSS_COMPILE}nm -n u-boot__image_copy_start, __image_copy_end, relocate_code symbol. 함수 선언 문자열을 검색하지 않는다.
bdinfo와 초기 로그RAM bank, relocaddr, new_gd, start_addr_sp
디버거 또는 QEMUboard_init_f 끝, relocate_code 진입·복귀, board_init_r의 PC/메모리

보호 영역을 지우지 않고 검산

  1. 01
    주소표 작성

    동일 빌드의 bdinfo와 map에서 ram_top, relocaddr, start_addr_sp를 별도 행으로 기록한다.

  2. 02
    전환 전후 비교

    디버거 또는 QEMU에서 setup_reloc() 직후와 ARM64 relocate_code() 뒤의 목적지 메모리를 비교해 복사 완료 시점을 확인한다.

  3. 03
    설정 한 항목만 바꿔 비교

    시험용 빌드에서 malloc 크기만 바꿔 start_addr_sp와 relocaddr 차이를 관찰한다. 보드의 보호 메모리 선언은 변경하지 않는다.

고정 U-Boot 소스와 다음 글