uimage.nix (2388B)
1 { 2 lzma, 3 stdenv, 4 ubootTools, 5 dtc, 6 lib, 7 }: 8 let 9 objcopy = "${stdenv.cc.bintools.targetPrefix}objcopy"; 10 arch = stdenv.hostPlatform.linuxArch; 11 stripAndZip = '' 12 ${objcopy} -O binary -R .reginfo -R .notes -R .note -R .comment -R .mdebug -R .note.gnu.build-id -S vmlinux.elf vmlinux.bin 13 rm -f vmlinux.bin.lzma ; lzma -k -z vmlinux.bin 14 ''; 15 in 16 { 17 kernel, 18 commandLine, 19 commandLineDtbNode ? "bootargs", 20 entryPoint, 21 extraName ? "", # e.g. socFamily 22 loadAddress, 23 imageFormat, 24 alignment ? null, 25 dtb ? null, 26 }: 27 stdenv.mkDerivation { 28 name = "kernel.image"; 29 phases = [ 30 "preparePhase" 31 ( 32 if commandLine != null then 33 assert dtb != null; 34 "mungeDtbPhase" 35 else 36 ":" 37 ) 38 (if imageFormat == "fit" then "buildPhaseFIT" else "buildPhaseUImage") 39 "installPhase" 40 ]; 41 nativeBuildInputs = [ 42 lzma 43 dtc 44 stdenv.cc 45 ubootTools 46 ]; 47 preparePhase = '' 48 cp ${kernel} vmlinux.elf; chmod +w vmlinux.elf 49 ''; 50 mungeDtbPhase = '' 51 dtc -I dtb -O dts -o tmp.dts ${dtb} 52 echo '/{ chosen { ${commandLineDtbNode} = ${builtins.toJSON commandLine}; }; };' >> tmp.dts 53 dtc -I dts -O dtb -o tmp.dtb tmp.dts 54 ''; 55 56 buildPhaseUImage = '' 57 test -f tmp.dtb && ${objcopy} --update-section .appended_dtb=tmp.dtb vmlinux.elf || ${objcopy} --add-section .appended_dtb=tmp.dtb vmlinux.elf 58 ${stripAndZip} 59 mkimage -A ${arch} -O linux -T kernel -C lzma -a 0x${lib.toHexString loadAddress} -e 0x${lib.toHexString entryPoint} -n '${lib.toUpper arch} Liminix Linux ${extraName}' -d vmlinux.bin.lzma kernel.uimage 60 ''; 61 62 buildPhaseFIT = '' 63 ${stripAndZip} 64 cat ${./kernel_fdt.its} > mkimage.its 65 cat << _VARS >> mkimage.its 66 / { 67 images { 68 kernel { 69 data = /incbin/("./vmlinux.bin.lzma"); 70 load = <0x${lib.toHexString loadAddress}>; 71 entry = <0x${lib.toHexString entryPoint}>; 72 arch = "${arch}"; 73 compression = "lzma"; 74 }; 75 fdt-1 { 76 data = /incbin/("./tmp.dtb"); 77 arch = "${arch}"; 78 }; 79 }; 80 }; 81 _VARS 82 mkimage -f mkimage.its -E ${ 83 lib.optionalString (alignment != null) "-B 0x${lib.toHexString alignment}" 84 } kernel.uimage 85 mkimage -l kernel.uimage 86 ''; 87 88 installPhase = '' 89 cp kernel.uimage $out 90 ''; 91 }