zombah

ac100toolkit by Tactilofan

Nov 12th, 2012
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.49 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. #This script has been made by Tactilofan and is under GPL 2.0 Licence
  4. # http://www.blogarm.net/ac100-toolkit/
  5.  
  6.  
  7. import os, re, sys,commands, time;
  8.  
  9.  
  10.  
  11.  
  12. #print USAGE file
  13. def print_help():
  14.     os.system("cat USAGE")
  15.  
  16. #********************************************************
  17. #********  md5 filecheck function ***********************
  18. #********************************************************
  19.  
  20.  
  21. def check_image_file(file_name):
  22.     check = 0
  23.     if os.path.isfile(file_name) and os.path.isfile(file_name+".md5sum"):
  24.         md5sum = commands.getoutput("md5sum -c "+file_name+".md5sum");
  25.         print md5sum
  26.         if re.match(file_name+": OK",md5sum):
  27.             check = 1
  28.     return check
  29.  
  30.  
  31.  
  32. #******************************************************************************
  33. #*************** NVFLASH simplified functions *********************************
  34. #******************************************************************************
  35.  
  36. #easyly executes nvflash
  37. def execute_nvflash(params):
  38.     nvflash_path = "./linux4tegra/nvflash/";
  39.     ret = os.system("sudo LD_LIBRARY_PATH="+nvflash_path+" "+nvflash_path+"nvflash "+params);
  40.     print "\n"
  41.     return ret
  42.  
  43. #execute nvflash and return the stdout
  44. def return_nvflash_stdout(params):
  45.     nvflash_path = "./linux4tegra/nvflash/";
  46.     return commands.getoutput("sudo LD_LIBRARY_PATH="+nvflash_path+" "+nvflash_path+"nvflash "+params);
  47.  
  48. #load the bootloader in ram
  49. def load_bootloader_in_ram():
  50.     return execute_nvflash("--bl ./linux4tegra/prebuilt/fastboot.stock.bin --go");
  51.  
  52.  
  53. #backup a partition
  54. def backup_partition(PartitionId,output_folder = ""):
  55.     nvflash_param = "-r --read "+str(PartitionId)+" " + output_folder+"tac100-part-"+str(PartitionId)+".img";
  56.     execute_nvflash(nvflash_param)
  57.  
  58. #downloads partition table
  59. def get_partition_table(output_file="./tac100-partitiontable.txt"):
  60.     execute_nvflash("-r --getpartitiontable "+output_file);
  61.    
  62. #write the given in the given partition number
  63. def restore_partition_from_file(partition_id,file_adress):
  64.     execute_nvflash("--resume --download "+str(partition_id)+" "+file_adress+ " --go")
  65.  
  66. def prepare_for_ubuntu():
  67.     print "done in args parsing"
  68.    
  69.    
  70.    
  71. #this function takes an partition table file at input and return a dictionnary with all partition data ordered by partition
  72. def read_partition_table(input_file):
  73.     #opens the file
  74.     partition_table_file = open(input_file);
  75.    
  76.     #removes the somes useless caracters
  77.     partition_table = partition_table_file.read().replace("\r","");
  78.     partition_table = partition_table.replace("\n\n","\n");
  79.    
  80.     #split the file into a list where each item of the list is a line
  81.     partition_table = partition_table.split("\n");
  82.     partition_dico = {}
  83.    
  84.     #reads all the lines
  85.     for line in partition_table:
  86.         if(line!=""):
  87.             #split the line to parse parameters and values
  88.             line = line.split("=");
  89.             field = line[0];
  90.             value = line[1];
  91.            
  92.             #if the line contain a new PartitionId, it create a new dictionary into the partition dictionary
  93.             if(field == "PartitionId"):
  94.                 current_partition = value;
  95.                 partition_dico[current_partition] = {};
  96.                
  97.             partition_dico[current_partition][field] = value;
  98.     return partition_dico
  99.    
  100.  
  101. def print_message(message):
  102.     print '************************************************************************'
  103.     print  message
  104.     print '************************************************************************'
  105.  
  106.  
  107. def wait_for_connection(current_partition, partition_number, total_partitions_count):
  108.     initial = os.popen("dmesg|grep -i nvidia|wc -l").read()
  109.     while 1:
  110.         if (os.system("sh -c \"grep -q 0955  /sys/bus/usb/devices/*/idVendor\"") == 0
  111.                 and (current_partition == 0 or initial != os.popen("dmesg|grep -i nvidia|wc -l").read())):
  112.                 print_message ("Connected to device! Going to write partition #" + str(partition_number))
  113.                 return
  114.         message = "Current partition #" + str(partition_number) + ", " + str(current_partition) + " of " + str(total_partitions_count);
  115.         message += " = " + str(total_partitions_count + 1 - current_partition) + " partition[s] remain.\n\n";
  116.         if os.system("sh -c \"grep -q 0955  /sys/bus/usb/devices/*/idVendor\"") == 0:
  117.                 message += "\nWaiting for reinitialization!\n\nPower off the device: press POWER button for more than 10 seconds;\n Then ";
  118.         message += "press CTRL + ESCAPE + POWER  on smartbook to get into recovery mode \n"
  119.         if os.system("sh -c \"grep -q 0955  /sys/bus/usb/devices/*/idVendor\"") == 0:
  120.                 message += "while the smartbook is still being powered off."
  121.         print_message (message)
  122.         time.sleep(2)
  123.  
  124. def rebuild_partition_table(android_version):
  125.     images_path = "./images/2.1"
  126.     if android_version == "2.2":
  127.         images_path = "./images/2.2"
  128.  
  129.     partitions = range(2, 14)
  130.     if len(sys.argv) > 3:
  131.             del partitions[:]
  132.             i = 3
  133.             while i < len(sys.argv):
  134.                 partitions.append(int(sys.argv[i]))
  135.                 i+=1
  136.  
  137.     counter = 0
  138.     for i in partitions:
  139.             counter += 1
  140.             ret = 1
  141.             while ret !=  0:
  142.                 image = images_path + "/tac100-part-" + str(i) + ".img"
  143.                 info = read_partition_table(images_path + "/tac100-partitiontable.txt")
  144.                 StartSector = info[str(i)]["StartSector"]
  145.                 NumSectors = info[str(i)]["NumSectors"]
  146.  
  147.                 wait_for_connection(counter, i, len(partitions))
  148.                 ret = load_bootloader_in_ram()
  149.                 if ret == 0:
  150.                     print_message('Rebuilding the partition #' + str(i) +  "\n./nvflash -r --rawdevicewrite "
  151.                             + StartSector + " " +  NumSectors + " " +  image)
  152.                     ret = execute_nvflash("-r --rawdevicewrite " + StartSector + " " + NumSectors + " "+ image)
  153.  
  154.        
  155. #**************************************************************
  156. #***********  Beginning of the script (args parsing ***********
  157. #***************************************************************
  158.  
  159. args = sys.argv;
  160.  
  161. if(len(args) > 1):
  162.     if(args[1] == "load-bootloader"):
  163.         load_bootloader_in_ram();
  164.     elif(args[1] == "backup"):
  165.         if(len(args)>3):
  166.             ouput_folder = args[3];
  167.         else:
  168.             output_folder = "";
  169.        
  170.         backup_partition(args[2]);
  171.        
  172.     elif(args[1] == "backup-all"):
  173.         print "\n * Loading the bootloader in RAM\n";
  174.         print "\n * Getting the partition table\n";
  175.         get_partition_table();
  176.        
  177.         for i in range(2,14):
  178.             print " * Backup of partition #",i,"\n"
  179.             backup_partition(i,"./");
  180.         os.system("sudo chmod 777 tac100*.img");
  181.         os.system("sudo chmod 777 tac100-partitiontable.txt");
  182.     elif(args[1] == "restore" and len(args)==4):
  183.         print " * Restoring partition  #",args[2],"\n"
  184.         restore_partition_from_file(args[2],args[3]);
  185.     elif args[1] == "build":
  186.         android_version = "2.1"
  187.         if len(sys.argv) > 2:
  188.                 android_version = sys.argv[2]
  189.         print " * full revert to android " + android_version + ": building partition table and writing data\n"
  190.         rebuild_partition_table(android_version);
  191.  
  192.     elif(args[1] == "prepare-for-ubuntu"):
  193.         #setting image name and address
  194.         image_name = "tegra-2.6.29-arm2-ac100-boot.img"
  195.         image_adress = "http://ac100.gudinna.com/";
  196.  
  197.  
  198.         #download the image until the md5 is okay
  199.         while check_image_file(image_name)==0:
  200.             #if one of the file already exists, it remove it by security.
  201.             if(os.path.isfile(image_name)):
  202.                 os.system("rm "+image_name);
  203.             if(os.path.isfile(image_name+".md5sum")):
  204.                 os.system("rm "+image_name+".md5sum");
  205.             #download the image
  206.             os.system("wget "+image_adress+image_name);
  207.             os.system("wget "+image_adress+image_name+".md5sum")
  208.  
  209.         #mount the image
  210.         restore_partition_from_file(5,image_name);
  211.        
  212.  
  213.     elif(args[1] == "install-nvflash"):
  214.         nvflash_address = "http://developer.download.nvidia.com/tegra/files/"
  215.         nvflash_filename = "linux_for_tegra_os_pack_rel_10.8.2_20100903.run.zip"
  216.        
  217.         #download nvflash from Nvidia site if it not exists
  218.         if os.path.isfile(nvflash_filename) == 0:
  219.             print " * Start downloading NvFlash"
  220.             os.system("wget "+nvflash_address+nvflash_filename);
  221.        
  222.         print "*NvFlash Downloaded"
  223.        
  224.         #install it
  225.         os.system("./setup-nvflash")
  226.         print " * NvFlash is installed"
  227.  
  228.     elif(args[1]=="nvf"): #option to execute a nvflash command
  229.        
  230.         command_to_execute = ""
  231.         #construct nvflash param line
  232.         for i in range(2,len(args)):
  233.             command_to_execute = command_to_execute + " " + str(args[i])
  234.        
  235.         execute_nvflash(command_to_execute)
  236.     else:
  237.         print_help();
  238. else:
  239.     print_help();
Advertisement
Add Comment
Please, Sign In to add comment