master
ps1 67 lines 2.26 KB
Raw
1 # Copyright (c) Microsoft Corporation.
2 # Licensed under the MIT License.
3
4 # Creates a CPIO newc format initramfs archive containing a single file named "init"
5 # with mode 0100755 (rwxr-xr-x), uid 0, gid 0.
6
7 [CmdletBinding()]
8 param (
9 [Parameter(Mandatory)][string]$InputFile,
10 [Parameter(Mandatory)][string]$OutputFile
11 )
12
13 $ErrorActionPreference = "Stop"
14 Set-StrictMode -Version Latest
15
16 function Write-Pad([System.IO.Stream]$Stream)
17 {
18 $remainder = $Stream.Position % 4
19 if ($remainder -ne 0)
20 {
21 $pad = [byte[]]::new(4 - $remainder)
22 $Stream.Write($pad, 0, $pad.Length)
23 }
24 }
25
26 function Write-CpioEntry([System.IO.Stream]$Stream, [byte[]]$NameBytes, [byte[]]$FileData, [int]$Mode, [uint32]$Mtime)
27 {
28 $header = "070701" + # header magic
29 "00000001" + # inode
30 ("{0:X8}" -f $Mode) + # mode
31 "00000000" + # uid
32 "00000000" + # gid
33 "00000001" + # nlink
34 ("{0:X8}" -f $Mtime) + # mtime
35 ("{0:X8}" -f $FileData.Length) + # filesize
36 "00000000" + # devmajor
37 "00000000" + # devminor
38 "00000000" + # rdevmajor
39 "00000000" + # rdevminor
40 ("{0:X8}" -f $NameBytes.Length) + # namesize
41 "00000000" # check
42 $headerBytes = [System.Text.Encoding]::ASCII.GetBytes($header)
43 $Stream.Write($headerBytes, 0, $headerBytes.Length)
44 $Stream.Write($NameBytes, 0, $NameBytes.Length)
45 Write-Pad $Stream
46 if ($FileData.Length -gt 0)
47 {
48 $Stream.Write($FileData, 0, $FileData.Length)
49 Write-Pad $Stream
50 }
51 }
52
53 $data = [System.IO.File]::ReadAllBytes($InputFile)
54 $name = [System.Text.Encoding]::ASCII.GetBytes("init`0")
55 $mtime = [uint32][System.DateTimeOffset]::UtcNow.ToUnixTimeSeconds()
56
57 $out = [System.IO.File]::Create($OutputFile)
58 try
59 {
60 Write-CpioEntry $out $name $data 0x81ED $mtime # S_IFREG | 0755
61 $trailer = [System.Text.Encoding]::ASCII.GetBytes("TRAILER!!!`0")
62 Write-CpioEntry $out $trailer ([byte[]]::new(0)) 0 0
63 }
64 finally
65 {
66 $out.Close()
67 }