uimage.nix (2334B)
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 kernel 17 , commandLine 18 , commandLineDtbNode ? "bootargs" 19 , entryPoint 20 , extraName ? "" # e.g. socFamily 21 , loadAddress 22 , imageFormat 23 , alignment ? null 24 , dtb ? null 25 } : stdenv.mkDerivation { 26 name = "kernel.image"; 27 phases = [ 28 "preparePhase" 29 (if commandLine != null then assert dtb != null; "mungeDtbPhase" else ":") 30 (if imageFormat == "fit" then "buildPhaseFIT" else "buildPhaseUImage") 31 "installPhase" 32 ]; 33 nativeBuildInputs = [ 34 lzma 35 dtc 36 stdenv.cc 37 ubootTools 38 ]; 39 preparePhase = '' 40 cp ${kernel} vmlinux.elf; chmod +w vmlinux.elf 41 ''; 42 mungeDtbPhase = '' 43 dtc -I dtb -O dts -o tmp.dts ${dtb} 44 echo '/{ chosen { ${commandLineDtbNode} = ${builtins.toJSON commandLine}; }; };' >> tmp.dts 45 dtc -I dts -O dtb -o tmp.dtb tmp.dts 46 ''; 47 48 buildPhaseUImage = '' 49 test -f tmp.dtb && ${objcopy} --update-section .appended_dtb=tmp.dtb vmlinux.elf || ${objcopy} --add-section .appended_dtb=tmp.dtb vmlinux.elf 50 ${stripAndZip} 51 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 52 ''; 53 54 buildPhaseFIT = '' 55 ${stripAndZip} 56 cat ${./kernel_fdt.its} > mkimage.its 57 cat << _VARS >> mkimage.its 58 / { 59 images { 60 kernel { 61 data = /incbin/("./vmlinux.bin.lzma"); 62 load = <0x${lib.toHexString loadAddress}>; 63 entry = <0x${lib.toHexString entryPoint}>; 64 arch = "${arch}"; 65 compression = "lzma"; 66 }; 67 fdt-1 { 68 data = /incbin/("./tmp.dtb"); 69 arch = "${arch}"; 70 }; 71 }; 72 }; 73 _VARS 74 mkimage -f mkimage.its ${lib.optionalString (alignment != null) "-B 0x${lib.toHexString alignment}"} kernel.uimage 75 mkimage -l kernel.uimage 76 ''; 77 78 installPhase = '' 79 cp kernel.uimage $out 80 ''; 81 }