Created
October 25, 2013 05:41
-
-
Save qianjigui/7149897 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# | |
# encoding: utf-8 | |
# | |
require 'rexml/document' | |
require 'erb' | |
=begin | |
#NAND | |
#MMC | |
mw.b 0x3000000 0x0 0x800000 | |
tftp 0x3000000 kernel.img | |
#mmc write 0 0x3000000 0x9600000 0x4000 | |
# 150M -> 150*1024^2 / 512 = 0x4B000 | |
mmc write 0 0x3000000 0x4B000 0x4000 | |
=end | |
# Get by mmcinfo | |
# Rd block len | |
EMMC_BLOCK_LEN = 512 | |
EMMC_DEVICE_NUM = 0 | |
class SizeFormatUnsupportError < StandardError; end | |
class PartitionInfo | |
def self.length_to_bytes(value) | |
value.strip! | |
value.downcase! | |
if /\A(\d+)([bkmg])*\Z/=~value | |
r = Regexp.last_match | |
base = r[1].to_i | |
base = base * 1024 ** case r[2] | |
when nil,'b' | |
0 | |
when 'k' | |
1 | |
when 'm' | |
2 | |
when 'g' | |
3 | |
end | |
base | |
else | |
raise SizeFormatUnsupportError.new | |
end | |
end | |
def length_to_bytes(length) | |
PartitionInfo.length_to_bytes(length) | |
end | |
def filesystem_to_format(filesystem) | |
case filesystem | |
when 'none' | |
{:suffix=>'.img', :writeopt=>'write'} | |
when 'ext3/4' | |
{:suffix=>'.ext4', :writeopt=>'write.ext4sp'} | |
end | |
end | |
attr_reader :name | |
def initialize(values) | |
@values = values | |
@name = @values['PartitionName'] | |
@start = length_to_bytes @values['Start'] | |
@length = length_to_bytes @values['Length'] | |
@type = @values['FlashType'] | |
@format = filesystem_to_format(@values['FileSystem']) | |
end | |
SPACE_COLUMNS = 25 | |
PARTITION_INFO_PART_TEMPLATE_DATA=<<END | |
<%= @name %> | |
FlashType: <%= @type %> | |
FileSystem: <%= @values['FileSystem'] %> | |
. | |
. | |
Length: <%= @values['Length'] %> | |
<%= 'Size: 0x%X bytes' % [@length] %> | |
<%= 'Block count 0x%X' % [to_block_count(@length)] %> | |
. | |
. | |
END | |
LINE_FORMAT="|%-#{SPACE_COLUMNS}s|" | |
def summary_lines | |
first = "+#{'-'*SPACE_COLUMNS}+ 0x#{@start.to_s(16)} @ block #0x#{to_block_count(@start).to_s(16)}" | |
template = ERB.new(PARTITION_INFO_PART_TEMPLATE_DATA) | |
lines = template.result(binding) | |
res = [first] | |
lines.split("\n").each do |line| | |
res << LINE_FORMAT % [line] | |
end | |
res<<'' | |
res.join("\n") | |
end | |
### fastboot operations | |
def clean_memory(addr) | |
'mw.b 0x%X 0x0 0x%X' % [addr, @length] | |
end | |
def tftp(addr) | |
'tftp 0x%X %s' % [ addr, @name+@format[:suffix] ] | |
end | |
def to_block_count(size) | |
((size*1.0)/EMMC_BLOCK_LEN).ceil | |
end | |
def write(addr) | |
case @type | |
when 'emmc' | |
'mmc %s %d 0x%X 0x%X 0x%X' % [@format[:writeopt], EMMC_DEVICE_NUM, addr, to_block_count(@start), to_block_count(@length)] | |
else | |
raise StandardError.new('No flash type support:'+@type) | |
end | |
end | |
EMMC_FLASH_SCRIPT_TEMPLATE=<<END | |
#Flash <%= @name %> | |
# FlashType: <%= @type %> | |
# FileSystem: <%= @values['FileSystem'] %> | |
# Start: <%= @values['Start'] %> @ block #0x<%= to_block_count(@start).to_s 16 %> | |
# Length: <%= @values['Length'] %> block length=0x<%= to_block_count(@length).to_s 16 %> | |
## Clean Memory | |
<%= clean_memory(addr) %> | |
## tftp image to Memory | |
<%= tftp(addr) %> | |
## Write disk from Memory | |
<%= write(addr) %> | |
###### END | |
END | |
def flash_script(addr) | |
template_data = case @type | |
when 'emmc' | |
EMMC_FLASH_SCRIPT_TEMPLATE | |
else | |
raise StandardError.new('No flash type support:'+@type) | |
end | |
template = ERB.new(template_data) | |
template.result(binding) | |
end | |
def flash(addr) | |
[ clean_memory(addr), tftp(addr), write(addr)].join("\n") | |
end | |
end | |
xmlfile = File.new('Hi3718CV100-emmc.xml') | |
xmldoc = REXML::Document.new(xmlfile) | |
partitions = [] | |
xmldoc.elements.each('Partition_Info/Part') do |e| | |
begin | |
info = PartitionInfo.new e.attributes | |
partitions << info | |
puts info.flash_script(0x3000000) | |
puts '' | |
puts '' | |
rescue SizeFormatUnsupportError | |
end | |
end | |
def summary(partitions) | |
res = [] | |
partitions.each do |partition| | |
res << partition.summary_lines | |
end | |
res << ('+'+'-'*PartitionInfo::SPACE_COLUMNS+'+') | |
res.join('') | |
end | |
puts summary(partitions) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment