Exemple #1
0
        //向磁盘写入数据
        private void Button3_Click(object sender, EventArgs e)
        {
            byte[] WriteByte = new byte[512];
            for (uint i = 0; i < 512; i++)
            {
                WriteByte[i] = 0xFF;
            }
            Listdata execute_data = new Listdata();

            //if (DiskOpen == false)
            //{
            //	MessageBox.Show(this, "磁盘未打开", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Question);
            //	return;
            //}

            if (MessageBox.Show(this, "确定要执行操作?此操作无法撤销!", "提示信息:", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) != DialogResult.Yes)
            {
                button3.Text = "执行";
                return;
            }

            button3.Text = "停止";

            for (int i = 0; i < listView1.CheckedItems.Count; i++)    //遍历整个列表
            {
                if (listView1.CheckedItems[i].Checked)                //判断是否复选
                {
                    execute_data.full_name  = listView1.Items[i].SubItems[6].Text;
                    execute_data.operating  = listView1.Items[i].SubItems[1].Text;
                    execute_data.file_start = Convert.ToUInt32(listView1.Items[i].SubItems[3].Text);
                    execute_data.disk_start = Convert.ToUInt32(listView1.Items[i].SubItems[4].Text);
                    execute_data.data_size  = Convert.ToUInt32(listView1.Items[i].SubItems[5].Text);

                    //写入磁盘
                    if (execute_data.operating == "写入" || execute_data.operating == "擦除")
                    {
                        if (execute_data.data_size == 0 && execute_data.operating == "写入")
                        {
                            MessageBox.Show(this, "文件无内容或不存在", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Question);
                            continue;
                        }

                        for (uint SurplusLen = 0; SurplusLen < execute_data.data_size; SurplusLen += 512)
                        {
                            if (execute_data.operating == "写入")
                            {
                                //读取数据
                                WriteByte = FileBin.BinRead(execute_data.full_name, SurplusLen + execute_data.file_start, 512);
                            }
                            //将数据写入流
                            cipan.WriteSector(WriteByte, (SurplusLen / 512) + execute_data.disk_start);

                            //将当前流中的数据写入磁盘
                            cipan.Refresh();

                            //更新进度条
                            progressBar1.Step = (int)(SurplusLen * 100 / execute_data.data_size);
                            progressBar1.PerformStep();
                        }
                        MessageBox.Show(this, "文件写入完成", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Question);
                    }
                    else if (execute_data.operating == "读取")
                    {
                        /*读取数据*/
                        for (uint SurplusLen = 0; SurplusLen < execute_data.data_size; SurplusLen += 512)
                        {
                            //读取磁盘数据
                            WriteByte = cipan.ReadSector((SurplusLen / 512) + execute_data.disk_start);

                            //写入文件
                            FileBin.Write(execute_data.full_name, (SurplusLen / 512) + execute_data.disk_start, WriteByte);

                            //更新进度条
                            progressBar1.Step = (int)(SurplusLen * 100 / execute_data.data_size);
                            progressBar1.PerformStep();
                        }
                        MessageBox.Show(this, "磁盘读取完成", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Question);
                    }
                }
            }
            button3.Text = "执行";
        }
Exemple #2
0
        private void button10_Click(object sender, EventArgs e)
        {
            /*
             * struct bpb_t {//biosParmBlock
             * char bytesPerSector_0;
             * char bytesPerSector_1;
             * char sectorsPerCluster;
             * char reservedSectorCount_0;
             * char reservedSectorCount_1;
             * char fatCount;
             * char rootDirEntryCount_0;
             * char rootDirEntryCount_1;
             * char totalSectors16_0;
             * char totalSectors16_1;
             *
             * char mediaType;
             *
             * char sectorsPerFat16_0;
             * char sectorsPerFat16_1;
             * char sectorsPerTrtack_0;
             * char sectorsPerTrtack_1;
             * char headCount_0;
             * char headCount_1;
             * char hidddenSectors_0;
             * char hidddenSectors_1;
             * char hidddenSectors_2;
             * char hidddenSectors_3;
             *
             * char totalSectors32_0;
             * char totalSectors32_1;
             * char totalSectors32_2;
             * char totalSectors32_3;
             * char sectorsPerFat32_0;
             * char sectorsPerFat32_1;
             * char sectorsPerFat32_2;
             * char sectorsPerFat32_3;
             * char fat32Flags_0;
             * char fat32Flags_1;
             *
             * char fat32Version_0;
             * char fat32Version_1;
             * char fat32RootCluster_0;
             * char fat32RootCluster_1;
             * char fat32RootCluster_2;
             * char fat32RootCluster_3;
             * char fat32FSInfo_0;
             * char fat32FSInfo_1;
             * char fat32BackBootBlock_0;
             * char fat32BackBootBlock_1;
             *
             * char fat32Reserved[12];
             * };41+12
             *
             * struct fbs_t {//fat32BootSector
             * char jmpToBootCode[3]; 3
             * char oemName[8]; 8
             * bpb_t bpb; 41+12
             *
             * char driveNumber;
             * char reserved1;
             * char bootSignature;
             * char volumeSerialNumber_0;
             * char volumeSerialNumber_1;
             * char volumeSerialNumber_2;
             * char volumeSerialNumber_3;  7
             *
             * char volumeLabel[11]; 11
             * char fileSystemType[8]; 8
             * char bootCode[420];
             * char bootSectorSig0;
             * char bootSectorSig1;
             * };
             * struct dir_t {//directoryEntry
             * char name[11];
             * char attributes;
             * char reservedNT;
             * char creationTimeTenths;
             * char creationTime_0;
             * char creationTime_1;
             *
             * char creationDate_0;
             * char creationDate_1;
             * char lastAccessDate_0;
             * char lastAccessDate_1;
             * char firstClusterHigh_0; 20
             * char firstClusterHigh_1;
             * char lastWriteTime_0;
             * char lastWriteTime_1;
             * char lastWriteDate_0;
             * char lastWriteDate_1;
             * char firstClusterLow_0; 26
             * char firstClusterLow_1;
             * char fileSize_0;
             * char fileSize_1;
             * char fileSize_2;
             * char fileSize_3;
             * };
             *
             */
            SDUtils sd = new SDUtils(this.textBox3.Text + ":");

            byte[] fbs = sd.ReadSector(0);
            int    volumeStartBlock    = 0;
            int    reservedSectorCount = fbs[3 + 8 + 3 + 0] | (fbs[3 + 8 + 3 + 1] << 8);
            int    fatStartBlock       = volumeStartBlock + reservedSectorCount;

            int fatCount     = fbs[3 + 8 + 5];
            int blocksPerFat = fbs[3 + 8 + 11 + 0] | (fbs[3 + 8 + 11 + 1] << 8);
            int rootDirStart = fatStartBlock + fatCount * blocksPerFat;

            int rootDirEntryCount = fbs[3 + 8 + 6 + 0] | (fbs[3 + 8 + 6 + 1] << 8);

            int dataStartBlock = rootDirStart + (((rootDirEntryCount << 5) + 511) >> 9);


            byte[] root = sd.ReadSector(rootDirStart);
            string name = "BOOT    BIN";
            //string name = "TEST1   TXT";
            int index = -1;

            for (int i = 0; i < 512; i += 32)
            {
                int match = 1;
                for (int j = 0; j < 11; j++)
                {
                    if (root[i + j] != (byte)name[j])
                    {
                        match = 0;
                        break;
                    }
                }
                if (match == 1)
                {
                    index = i;
                }
            }

            int firstCluster;
            int fileSize;

            if (index >= 0)
            {
                firstCluster = root[index + 26 + 0] | (root[index + 26 + 1] << 8) | (root[index + 20 + 0] << 16) | (root[index + 20 + 1] << 24);
                fileSize     = root[index + 28 + 0] | (root[index + 28 + 1] << 8) | (root[index + 28 + 2] << 16) | (root[index + 28 + 3] << 24);
            }
            else
            {
                throw new Exception("not found boot.bin");
            }

            short[] fat = new short[blocksPerFat * 256];
            for (int i = 0; i < blocksPerFat; i++)
            {
                byte[] fattemp = sd.ReadSector(fatStartBlock + i);
                for (int j = 0; j < 256; j++)
                {
                    fat[i * 256 + j] = (short)(fattemp[j * 2] + (fattemp[j * 2 + 1] << 8));
                }
            }

            int blocks = 0;
            int start  = firstCluster;

            while (true)
            {
                int oldStart = start;
                start = fat[start];
                blocks++;
                if (start == -1)
                {
                    break;
                }
                if (start != oldStart + 1)
                {
                    throw new Exception("not continue block");
                }
            }

            int sectorsPerCluster = fbs[3 + 8 + 2];

            int blockOfCluster = 0;
            int firstBlock     = dataStartBlock + (firstCluster - 2) * sectorsPerCluster + blockOfCluster;

            byte[] data = sd.ReadSector(firstBlock);


            int bootrec = 3 + 8 + 41 + 12 + 7 + 11 + 8;

            fbs[bootrec + 0] = 0x12;
            fbs[bootrec + 1] = 0x34;
            fbs[bootrec + 2] = 0x56;
            fbs[bootrec + 3] = 0x78;

            fbs[bootrec + 4] = (byte)((firstBlock >> 0) & 0xFF);
            fbs[bootrec + 5] = (byte)((firstBlock >> 8) & 0xFF);
            fbs[bootrec + 6] = (byte)((firstBlock >> 16) & 0xFF);
            fbs[bootrec + 7] = (byte)((firstBlock >> 24) & 0xFF);

            fbs[bootrec + 8]  = (byte)((blocks >> 0) & 0xFF);
            fbs[bootrec + 9]  = (byte)((blocks >> 8) & 0xFF);
            fbs[bootrec + 10] = (byte)((blocks >> 16) & 0xFF);
            fbs[bootrec + 11] = (byte)((blocks >> 24) & 0xFF);

            sd.WriteSector(fbs, 0);
            MessageBox.Show("ok");
            sd.Close();
        }