private void gen_tmp(BinType type)
        {
            Image_header header = new Image_header();

            switch (type)
            {
            case BinType.BOOTLOADER:
                header.dest_addr = 0xf8000000;
                break;

            case BinType.BIN:
                header.dest_addr = 0x0;
                break;

            default:
                throw new Exception("BinType err\n");
            }
            if (type == BinType.BOOTLOADER)
            {
                tool.BuildPackage(header, bootload_file, tmp_blf);
            }
            if (type == BinType.BIN)
            {
                tool.BuildPackage(header, bin_file, tmp_bin);
            }
        }
        private void DownLoad_Click(object sender, RoutedEventArgs e)
        {
            Image_header header = new Image_header();

            if (!IsConnected)
            {
                MessageBox.Show("未检测到设备,不能进行下载!");
                return;
            }
            DebugInfo = "";
            try
            {
                //download.IsEnabled = false;
                UpdateProgressBar(0);
                Task.Factory.StartNew(() =>
                {
                    try
                    {
                        gen_tmp(BinType.BOOTLOADER);
                        UpdateProgressBar(25);
                        gen_tmp(BinType.BIN);
                        UpdateProgressBar(50);
                        send_file(tmp_blf);
                        UpdateProgressBar(75);
                        Thread.Sleep(4000);
                        send_file(tmp_bin);
                        UpdateProgressBar(100);
                    }
                    catch (Exception ex)
                    {
                        Dispatcher.BeginInvoke(new Action(() =>
                        {
                            pb.Visibility      = Visibility.Hidden;
                            download.IsEnabled = true;
                        }));
                        MessageBox.Show(ex.Message);
                    }
                });
            }catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
Exemple #3
0
        public bool BuildPackage(Image_header header, string orginfile, string tmppath)
        {
            int      total_len  = 0;
            bool     ret        = true;
            FileInfo fi         = new FileInfo(orginfile);
            int      param_len  = 0;
            int      file_align = 1;
            int      para_align = 1;

            total_len = (int)fi.Length;
            int case_align_len = 0;
            int para_align_len = 0;
            int para_len       = param_len;
            int mod            = total_len % file_align;

            if (mod != 0)
            {
                //补齐8字节
                case_align_len = file_align - mod;
            }

            mod = para_len % para_align;
            if (mod != 0)
            {
                //补齐4字节
                para_align_len = para_align - mod;
            }
            byte[] image_head_bytes = null;
            header.image_len       = total_len + case_align_len + para_len + para_align_len; //镜像密文数据大小
            header.image_plain_len = total_len + case_align_len + para_len + para_align_len; //镜像明文数据大小
            image_head_bytes       = header.ToBytes();

            int head_hash_len = header.Size;
            int hash_len      = head_hash_len + header.image_plain_len;

            FileStream fs        = new FileStream(orginfile, FileMode.Open);
            int        left      = (int)fs.Length;
            int        file_size = (int)fs.Length;
            int        size      = 0;

            byte[] sha256 = new byte[header.Size + left];

            Array.Copy(image_head_bytes, 0, sha256, 0, header.Size);
            while (left > 0)
            {
                size = left > 4096 ? 4096 : (int)left;
                fs.Read(sha256, header.Size + (file_size - left), size);
                left -= 4096;
            }
            fs.Close();
            SHA256Managed Sha256 = new SHA256Managed();

            byte[] sha256hash = Sha256.ComputeHash(sha256);
            byte[] head       = new byte[4096];
            Array.Clear(head, 0, 4096);
            Array.Copy(image_head_bytes, 0, head, 0, header.Size);
            Array.Copy(sha256hash, 0, head, head_hash_len + ROTPK_LENGTH, 32);

            /*
             * 头写入文件
             */
            FileStream outfs = new FileStream(tmppath, FileMode.OpenOrCreate);

            outfs.Write(head, 0, HEAD_SIZE);

            /*
             * 文件内容写入 tmppath
             */
            outfs.Write(sha256, header.Size, file_size);
            outfs.Close();

            return(ret);
        }