/[AnywhereTS-MSSQL]/trunk/TSAdminTool/mknbi.cs
ViewVC logotype

Contents of /trunk/TSAdminTool/mknbi.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4 - (show annotations) (download)
Wed Jul 11 14:19:13 2012 UTC (7 years, 10 months ago) by william
File size: 5734 byte(s)
imported from https://anywherets.svn.sourceforge.net/svnroot/anywherets/trunk/
Revision: 1
Author: beartown
Date: 3:10:40 AM, Monday, June 21, 2010
Message:
First upload. Still todo in order to get a fully working product: Replace Thinstation images with vanilla Thinstation, modify ImageDesigntimeConfig.cs, ImageRuntimeConfig.cs in order to support the Thinstation image
1 using System;
2 using System.IO;
3
4 public class NBI {
5
6
7 public static void XMain(string[] arg)
8 {
9 Console.WriteLine("NBI mejker 0.01");
10 String append = "rw root=/dev/ram0 ramdisk_blocksize=4096 dumbcon=4";
11
12 makeNBI(append);
13 Console.WriteLine("ennbi mejkad, alles klar");
14 }
15
16
17 //
18 // makeNBI
19 // creates an Etherboot NBI (Network Boot Image) file called 'client' in current directory
20 // Requires the files 'krnl32' (the Linux kernel), 'ldr32' (a NBI bootstrap loader
21 // from mknbi-1.4, GPL:d) and 'initrd' (the squashfs:ed ramdisk)
22 // Parameters: append: the kernel append string
23 //
24 // TODO:
25 // - Error checking (missing files, too small files, values out of bounds)
26 // - Uses some quite large (~10MB or so) buffers unnecessarily
27 // - Proper path support; the data-files should be in the data-dir etc
28 //
29 public static void makeNBI(String append)
30 {
31
32 FileStream strmKernel = new FileStream("krnl32", FileMode.Open, FileAccess.Read);
33 uint kernelSize = (uint)strmKernel.Length; // Bootsector + bootstrap + Kernel
34 uint kernelSizePadded512 = (uint)((kernelSize -0x200 -0x1c00 + 0x1FF)&~0x1FF); // Kernel, padded to 512b
35 uint kernelSizePadded4k = (uint)((kernelSize -0x200 -0x1c00 + 0xFFF)&~0xFFF); // Kernel, padded to 4096b
36 uint kernelSizeTotal = kernelSizePadded512 + 0x200 + 0x1c00;
37
38 // Extract size of bootstrap segment. This value is likely 14*512 = 7168,
39 // and the code assumes this value to be <=8192
40 BinaryReader kernelReader = new BinaryReader(strmKernel);
41 kernelReader.BaseStream.Seek(0x1f1, SeekOrigin.Begin);
42 uint bootstrapSize = (uint)(kernelReader.ReadByte() * 512);
43 kernelReader.BaseStream.Seek(0, SeekOrigin.Begin);
44
45 FileStream strmRamdisk = new FileStream("initrd", FileMode.Open, FileAccess.Read);
46 uint ramdiskSize = (uint)strmRamdisk.Length;
47 uint ramdiskSizePadded = (uint)((ramdiskSize + 0x1FF)&~0x1FF); // Padded to 512b
48
49 FileStream strmLoader = new FileStream("ldr32", FileMode.Open, FileAccess.Read);
50 uint loaderSize = (uint)strmLoader.Length;
51 uint loaderSizePadded = (uint)((loaderSize + 0x1FF)&~0x1FF); // Padded to 512b
52
53 uint[] nbiHeader = new uint[128]; // 128*4=512 byte header
54 int p=0;
55
56 nbiHeader[p++] = 0x1b031336; // Magic NBI header value
57 //nbiHeader[p++] = 0x80000054; // either: ......5. with 5*4 bytes vendor string
58 nbiHeader[p++] = 0x80000004; // or: ......0. with 0*4 bytes vendor string
59 nbiHeader[p++] = 0x82200000;
60 nbiHeader[p++] = 0x00082800;
61
62 // optional: Vendor string
63 //nbiHeader[p++] = 0x626e6b6d; // 'mknbi-linux-1.4.4'
64 //nbiHeader[p++] = 0x696c2d69;
65 //nbiHeader[p++] = 0x2d78756e;
66 //nbiHeader[p++] = 0x2e342e31;
67 //nbiHeader[p++] = 0x00000034;
68
69 // Type 0x10 (16): first32
70 nbiHeader[p++] = 0x00001004;
71 nbiHeader[p++] = 0x00082800;
72 nbiHeader[p++] = loaderSizePadded;
73 nbiHeader[p++] = 0x00001800; // Maxlen 6k
74
75 // Type 0x11 (17): APPEND
76 nbiHeader[p++] = 0x00001104;
77 nbiHeader[p++] = 0x00082400;
78 nbiHeader[p++] = 0x00000200; // Minimum size 512 bytes, and never bigger
79 nbiHeader[p++] = 0x00000800; // Maxlen 2k
80
81 // Type 0x12 (18): kernel bootsector
82 nbiHeader[p++] = 0x00001204;
83 nbiHeader[p++] = 0x00080000;
84 nbiHeader[p++] = 0x00000200; // Always 512 bytes
85 nbiHeader[p++] = 0x00000200; // Always 512 bytes
86
87 // Type 0x13 (19): kernel bootstrap
88 nbiHeader[p++] = 0x00001304;
89 nbiHeader[p++] = 0x00080200;
90 nbiHeader[p++] = bootstrapSize;
91 nbiHeader[p++] = 0x00002000; // Maxlen 8k
92
93 // Type 0x14 (20): kernel
94 nbiHeader[p++] = 0x00001404;
95 nbiHeader[p++] = 0x00100000; // Bigkernels start on 0x100000
96 nbiHeader[p++] = kernelSizePadded512;
97 nbiHeader[p++] = kernelSizePadded512; // Maxlen whatever the kernel size is
98
99 // Type 0x15 (21): initrd
100 nbiHeader[p++] = 0x04001514;
101 nbiHeader[p++] = 0x00100000 + kernelSizePadded4k; // Ramdisk starts at kernel end padded to 4k
102 nbiHeader[p++] = ramdiskSizePadded;
103 nbiHeader[p++] = ramdiskSizePadded;
104 nbiHeader[p++] = 0x00000000; // Vendor string zero?
105
106 nbiHeader[127] = 0xAA550000; // Section end-marker. AFAIK not strictly necessary.
107
108 // U*G*L*Y: This just copies an int[] array to a byte[] array, because BinaryWriter.Write must have an byte[]
109 byte[] buf = new byte[512];
110 for( int i=0; i<128; i++) {
111 buf[4*i+3] = (byte)((nbiHeader[i] & 0xFF000000) >> 24);
112 buf[4*i+2] = (byte)((nbiHeader[i] & 0x00FF0000) >> 16);
113 buf[4*i+1] = (byte)((nbiHeader[i] & 0x0000FF00) >> 8);
114 buf[4*i+0] = (byte)((nbiHeader[i] & 0x000000FF) >> 0);
115 }
116
117 FileStream strmNBI = new FileStream("client", FileMode.OpenOrCreate, FileAccess.Write);
118 BinaryWriter writeNBI = new BinaryWriter(strmNBI);
119 writeNBI.Write(buf, 0, 512);
120
121 // Write ldr32
122 byte[] loaderBuf = new byte[loaderSizePadded];
123 BinaryReader readLoader = new BinaryReader(strmLoader);
124 readLoader.Read(loaderBuf, 0, (int)loaderSize);
125 writeNBI.Write(loaderBuf, 0, (int)loaderSizePadded);
126
127 // Write APPEND
128 byte[] appendBuf = new byte[512];
129 char[] appendChars = append.ToCharArray();
130 for (int i=0;i<append.Length; i++)
131 appendBuf[i] = (byte)appendChars[i];
132 writeNBI.Write(appendBuf, 0, 512);
133
134 // Write kernel
135 byte[] kernelBuf = new byte[kernelSizeTotal];
136 BinaryReader readKernel = new BinaryReader(strmKernel);
137 readKernel.Read(kernelBuf, 0, (int)kernelSize);
138 writeNBI.Write(kernelBuf, 0, (int)kernelSizeTotal);
139
140 // Write ramdisk
141 byte[] ramdiskBuf = new byte[ramdiskSizePadded];
142 BinaryReader readRamdisk = new BinaryReader(strmRamdisk);
143 readRamdisk.Read(ramdiskBuf, 0, (int)ramdiskSize);
144 writeNBI.Write(ramdiskBuf, 0, (int)ramdiskSizePadded);
145
146 strmNBI.Flush();
147 strmNBI.Close();
148
149 strmLoader.Close();
150 strmRamdisk.Close();
151 strmLoader.Close();
152 }
153
154 }

  ViewVC Help
Powered by ViewVC 1.1.22