MENU

Catalog

    用Java做一个最小的操作系统内核

    November 18, 2018 • Read: 743 • 操作系统

    操作系统,是计算机科学中的皇冠,它作为一个平台,几乎集结了计算机科学里面的所有知识,如硬件,算法,架构,能够写出一个操作系统的人,无一不是行业里的翘楚或大神级人物,像Linux的创始人Torvalds,Android的创始人安迪·鲁宾,苹果早期电脑系统的开发者史蒂夫-沃兹尼亚克

    所以我也要做一个操作系统......

    import java.io.DataOutputStream;
    import java.io.FileOutputStream;
    import java.util.ArrayList;
    
    public class OperatingSystem {
        private ArrayList<Integer> imgByteToWrite = new ArrayList<Integer>();
        
        private int[] imgContent = new int[]{
            0xeb,0x4e,0x90,0x48,0x45,0x4c,0x4c,0x4f,0x49,0x50,0x4c,0x00,0x02,0x01,0x01,0x00,0x02,0xe0,
            0x00,0x40,0x0b,0xf0,0x09,0x00,0x12,0x00,0x02,0x00,0x00,0x00,0x00,0x00,0x40,0x0b,0x00,0x00,0x00,0x00,0x29,
            0xff,0xff,0xff,0xff,0x48,0x45,0x4c,0x4c,0x4f,0x2d,0x4f,0x53,0x20,0x20,0x20,0x46,0x41,0x54,0x31,0x32,
            0x20,0x20,0x20,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xb8,0x00,0x00,0x8e,
            0xd0,0xbc,0x00,0x7c,0x8e,0xd8,0x8e,0xc0,0xbe,0x74,0x7c,0x8a,
            0x04,0x83,0xc6,0x01,0x3c,0x00,0x74,0x09,0xb4,0x0e,0xbb,0x0f,0x00,0xcd,0x10,0xeb,0xee,0xf4,0xeb,0xfd
        };
    
        public OperatingSystem(String s) {
            for (int i = 0; i < imgContent.length; i++)
                imgByteToWrite.add(imgContent[i]);
            imgByteToWrite.add(0x0a);
            imgByteToWrite.add(0x0a);
            for (int j = 0; j < s.length(); j++)
                imgByteToWrite.add((int)s.charAt(j));
            imgByteToWrite.add(0x0a); //换行
            int len = 0x1fe;
            int curSize = imgByteToWrite.size();
            for (int k = 0; k < len - curSize; k++)
                imgByteToWrite.add(0);
    
            /*
             * 要想让虚拟机将软盘的头512字节当作操作系统的内核加载到内存
             * 前512字节的最后两个字节必须是55,aa
             */
            imgByteToWrite.add(0x55);
            imgByteToWrite.add(0xaa);
            imgByteToWrite.add(0xf0);
            imgByteToWrite.add(0xff);
            imgByteToWrite.add(0xff);
    
            len = 0x168000;
            curSize = imgByteToWrite.size();
            for (int l = 0; l < len - curSize; l++) // 其余部分都用0补全
                imgByteToWrite.add(0);
        }
    
        public void makeFllopy()   {
            /*
             * 在磁盘上创建一个1474560字节的二进制文件,将imgContent的内容写入该文件的前512字节
             * 这个二进制文件将作为一个1.5M的虚拟软盘用于当作虚拟机的启动软盘
             */
            try {
                DataOutputStream out = new DataOutputStream(new FileOutputStream("system.img"));
                for (int i = 0; i < imgByteToWrite.size(); i++)
                    out.writeByte(imgByteToWrite.get(i).byteValue());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        public static void main(String[] args) {
            OperatingSystem op = new OperatingSystem("hello, this is my first line of my operating system code");
            op.makeFllopy();
        }
    }

    上面的代码执行后,在工程目录下会生成一个system.img文件。接着利用virtualbox创建一个虚拟机,设置它为磁盘启动,并在配置中,将代码生成的system.img当做虚拟磁盘插入虚拟机:

    然后点击启动,结果如下:

    Java代码中,main函数里的字符串出现在了虚拟机屏幕里,然后虚拟机就卡死了。机器上电后就加载了我们的代码,这不就是一个操作系统的雏形吗。

    这是我操作系统的第一篇文章,接下来我会继续完善这个操作系统,敬请期待......

    Archives Tip
    QR Code for this page
    Tipping QR Code
    Leave a Comment

    已有 1 条评论
    1. Capella Capella

      老王牛逼!