⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mkimage.pl

📁 采用ST20 CPU的机顶盒的烧写程序
💻 PL
📖 第 1 页 / 共 5 页
字号:
          if (defined($stcValue) && $stcValue != 0)
          {
            $cmd .= " -stc $stcAddr $stcValue";
          }
          if (defined($vccValue) && $vccValue != 0)
          {
            $cmd .= " -vcc $vccAddr $vccValue";
          }
          if ($verboseOutput)
          {
            $cmd .= " -V$verboseOutput";
            print("Running command: $cmd\n")
          }
          my $err = system($cmd);
          die("failed to run signrl.pl ($!), stopped") if ($err);
          $rlFile = "signed_$rlFile";
          print("Generated Cryptocore signed file $rlFile\n") if ($verboseOutput);
        }
        else
        {
          die("$rlFile cannot be automatically Cryptocore signed for the $options{t} - " .
              "please use signrl.pl to sign it before passing it to $THIS_SCRIPT, " .
              "stopped");
        }
      }
      else
      {
        print("Don't re-sign $rlFile - it has already been signed\n") if ($verboseOutput);
      }
    }
  }

  # A better solution for ELF stripping would be useful so that only the necessary
  # information remains in the RL file before it is inserted into the .img file -
  # objcopy and strip are not feature rich enough to remove, for example, the
  # section tables.

  # We now have the (signed if needed) RL file - strip it and turn it into a .img
  my $cmd = getObjcopy() . " --strip-unneeded $rlFile $tmpRLFile";
  print("Running command: $cmd\n") if ($verboseOutput);
  my $err = system($cmd);
  if ($err)
  {
    # We continue using the unstripped ELF file if the strip failed, as in R3.0.2
    # of the ST40 toolset it always failed
    warn("Warning: " . getObjcopy() . " could not strip debug information from $rlFile - " .
         "debug information will be kept in the .img file.\n");
    $tmpRLFile = $rlFile;
  }

  # Put the .img file together
  my $rlSize = (stat($tmpRLFile))[7];
  my $secLen = $rlSize;

  unless (exists($options{d}))
  {
    my @time = localtime();
    $options{d} = "reloc ELF from $rlFile (" . 
                  sprintf("%02d/%02d/%02d, %02d:%02d", $time[3], $time[4], 
                          (($time[5] > 100) ? ($time[5] - (int($time[5]/100)*100)): $time[5]),
                          $time[2], $time[1]) .
                  ")";
  }

  open(OUT_FILE, ">$options{o}") or die("unable to open '$options{o}' ($!), stopped");
  binmode(OUT_FILE);
  #
  # The header...
  #
  my $fileHeader = pack('VZ32VVVV',
                        $IMAGE_FILE_MAGIC_NUM,
                        $options{d},
                        $options{c} | $architecture | $IMAGE_TYPE_RELOC_LIB,
                        0,       # Stack addr
                        $rlSize, # Entrypoint - for RLs we put in size (uncompressed) in bytes
                        1);      # Num of sections

  syswrite(OUT_FILE, $fileHeader, length($fileHeader)) or die("unable to write to '$options{o}' ($!), stopped");

  my $sectionType = $SECTION_TYPE_COPY;
  my $secData;
  if ($options{p} eq "deflate")
  {
    $sectionType = $SECTION_TYPE_DEFLATED;
    my $tmpDeflatedFile = createtmpname();

    # Init compressor
    my $stream = deflateInit(-Level => Z_BEST_COMPRESSION()) or die("unable to create deflation stream, stopped");
    my $status;
    my $buffer;

    # Read in uncompressed data to memory
    $buffer = readBinFileToStr($tmpRLFile);

    # Compress
    ($secData, $status) = $stream->deflate($buffer) ;
    $status == Z_OK() or die("deflation failed, stopped");

    my $moreData;
    ($moreData, $status) = $stream->flush() ;
    $status == Z_OK() or die("deflation flush failed, stopped");
    $secData .= $moreData;
    $secLen = length($secData);
  }
  else
  {
    $secData = readBinFileToStr($tmpRLFile);
  }

  # The section table...
  my $secDesc = pack('VVVV',
                     $PLACEMENT_TYPE_ANYWHERE | $sectionType,
                     0,
                     0,
                     $secLen);
  syswrite(OUT_FILE, $secDesc, length($secDesc)) or die("unable to write to '$options{o}' ($!), stopped");

  # The section data...
  syswrite(OUT_FILE, $secData, $secLen) or die("unable to write to '$options{o}' ($!), stopped");

  close(OUT_FILE);

  if ($verboseOutput)
  {
    print("\nGenerated $options{o} from $rlFile.\n");
  }
}


#
# Routine to decompose an ST20 romimage file into an image file.
# We support Motorola SRec format and ST hex files.
# Support for this file type is provided for the STi5528 which contains
# an ST20 processor. ST20 does not use the normal bootvectors at the
# bottom of FLASH. Instead it boots at the fixed address of 0x7ffffffe.
#
# ST20 ROM images contain one section - the 1st 256 byte block contains the
# bootstrap, and the rest is the application image. The section has to be
# placed in FLASH at the address indicated. When the image is booted the 1st
# block of the image is remapped by STi5528 hardware to appear at 0x7fffff00.
#
sub processST20File($$)
{
  my $fileFormat   = shift;
  my $fileName     = shift;
  my %sections     = ();
  my $nextAddress  = undef;
  my $sectionStart = undef;

  open(HANDLE, "<".$fileName) or die("unable to open '$fileName' ($!), stopped");

  #
  # Read in the file, storing consecutive records in a hash table,
  # where the key is the base address of the section.
  #
  while (<HANDLE>)
  {
    my $length  = undef;
    my $address = undef;
    my $data    = undef;
    my $chksum  = undef;

    if ($fileFormat eq "srec")
    {
      if (/^S3([[:xdigit:]]{2})([[:xdigit:]]{8})([[:xdigit:]]+)([[:xdigit:]]{2})\r?$/)
      {
        ($length, $address, $data, $chksum) = (hex($1), hex($2), $3, $4);
      }
      else
      {
        if (!((/^S0.*$/) || (/^S9.*$/)))
        {
          die("invalid S-record, stopped");
        }
      }
    }
    else
    {
      if (/^([[:xdigit:]]{8})(.+)$/)
      {
        ($address, $data) = (hex($1), $2);
        $data =~ s/\s+//g;
      }
      else
      {
        die("invalid hex record, stopped");
      }
    }

    if (defined($address))
    {
      unless (defined($sectionStart))
      {
        if ($address & 0xff)
        {
          die("expected image to start on 256 byte boundary, stopped");
        }

        $sectionStart            = $address;
        $sections{$sectionStart} = $data;
      }
      else
      {
        my $padding = $address - $nextAddress;

        if ($padding > 0)
        {
          $sections{$sectionStart} .= "55" x $padding;
        }
        elsif ($padding < 0)
        {
          die("addresses went backwards, stopped");
        }

        $sections{$sectionStart} .= $data;
      }

      $nextAddress = $address + (length($data) / 2);
    }
  }

  close(HANDLE);

  unless (exists($options{d}))
  {
    my @time = localtime();
    $options{d} = "srec/hex from $fileName (" . 
                  sprintf("%02d/%02d/%02d, %02d:%02d", $time[3], $time[4], 
                          (($time[5] > 100) ? ($time[5] - (int($time[5]/100)*100)): $time[5]),
                          $time[2], $time[1]) .
                  ")";
  }

  #
  # Now we've got all the data in, we can emit the file header,
  # followed by the sections, followed by the actual data.
  #
  open(HANDLE, ">$options{o}") or die("unable to open '$options{o}' ($!), stopped");
  binmode(HANDLE);
  #
  # The header...
  #
  my $fileHeader = pack('VZ32VVVV',
                        $IMAGE_FILE_MAGIC_NUM,
                        $options{d},
                        $options{c} | $CPU_ARCH_ST20,
                        0x00000000,
                        translate5528($sectionStart),
                        1);

  syswrite(HANDLE, $fileHeader, length($fileHeader)) or die("unable to write to '$options{o}' ($!), stopped");

  #
  # The section table...
  #
  while (my ($key, $value) = each (%sections))
  {
    my $section = pack('VVVV',
                       $PLACEMENT_TYPE_INPLACE | $SECTION_TYPE_SKIP,
                       translate5528($key),
                       translate5528($key),
                       length($value) / 2);
    syswrite(HANDLE, $section, length($section)) or die("unable to write to '$options{o}' ($!), stopped");
    $nextAddress = $key + (length($value) / 2);
  }

  #
  # The section data...
  #
  while (my ($key, $value) = each (%sections))
  {
    my $data = pack('H*', $value);

    syswrite(HANDLE, $data, length($data)) or die("unable to write to '$options{o}' ($!), stopped");
  }

  close(HANDLE);

  if ($verboseOutput)
  {
    print("\nGenerated $options{o} from $fileName.\n");
  }
}

#
# Routine to create an image file with a single blank section
#
sub createBlankImage()
{
  open(HANDLE, ">$options{o}") or die("unable to open '$options{o}' ($!), stopped");
  binmode(HANDLE);

  #
  # Figure out params
  #
  my $start;
  my $length;

  if (($start, $length) = $options{z} =~ m/^(0x[[:xdigit:]]+),(0x[[:xdigit:]]+)$/)
  {
    $start  = hex($start);
    $length = hex($length);
  }
  elsif (($start, $length) = $options{z} =~ m/^([[:digit:]]+),(0x[[:xdigit:]]+)$/)
  {
    $length = hex($length);
  }
  elsif (($start, $length) = $options{z} =~ m/^(0x[[:xdigit:]]+),([[:digit:]]+)$/)
  {
    $start  = hex($start);
  }
  elsif (($start, $length) = $options{z} =~ m/^([[:digit:]]+),([[:digit:]]+)$/)
  {
  }
  else
  {
    usage("-z $options{z} incorrectly specified, must be -z address,length");
  }

  unless (exists($options{d}))
  {
    my @time = localtime();
    $options{d} = "blank (" . 
                  sprintf("%02d/%02d/%02d, %02d:%02d", $time[3], $time[4], 
                          (($time[5] > 100) ? ($time[5] - (int($time[5]/100)*100)): $time[5]),
                          $time[2], $time[1]) .
                  ")";
  }
  #
  # The header...
  #
  my $fileHeader = pack('VZ32VVVV',
                        $IMAGE_FILE_MAGIC_NUM,
                        $options{d},
                        $IMAGE_TYPE_DATA,
                        0x00000000,
                        0x00000000,
                        1);

  syswrite(HANDLE, $fileHeader, length($fileHeader)) or die("unable to write to '$options{o}' ($!), stopped");

  my $section = pack('VVVV',
                     $PLACEMENT_TYPE_INPLACE_BLANK | $SECTION_TYPE_SKIP,
                     $start,
                     $start,
                     $length);

  syswrite(HANDLE, $section, length($section)) or die("unable to write to '$options{o}' ($!), stopped");

  close(HANDLE);

  if ($verboseOutput)
  {
    printf("\nGenerated $options{o} containing 1 blank section @ 0x%08x, length 0x%08x\n", $start, $length);
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -