chris41g

i9003 bmlutils.c

Jul 5th, 2011
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.96 KB | None | 0 0
  1. #include <ctype.h>
  2. #include <errno.h>
  3. #include <fcntl.h>
  4. #include <getopt.h>
  5. #include <limits.h>
  6. #include <linux/input.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <sys/reboot.h>
  11. #include <sys/types.h>
  12. #include <time.h>
  13. #include <unistd.h>
  14.  
  15. #include <sys/wait.h>
  16. #include <sys/limits.h>
  17. #include <dirent.h>
  18. #include <sys/stat.h>
  19.  
  20. #include <signal.h>
  21. #include <sys/wait.h>
  22.  
  23. extern int __system(const char *command);
  24. #define BML_UNLOCK_ALL              0x8A29      ///< unlock all partition RO -> RW
  25.  
  26.  
  27. static int restore_internal(const char* bml, const char* filename)
  28. {
  29.     char buf[4096];
  30.     int dstfd, srcfd, bytes_read, bytes_written, total_read = 0;
  31.     if (filename == NULL)
  32.         srcfd = 0;
  33.     else {
  34.         srcfd = open(filename, O_RDONLY | O_LARGEFILE);
  35.         if (srcfd < 0)
  36.             return 2;
  37.     }
  38.     dstfd = open(bml, O_RDWR | O_LARGEFILE);
  39.     if (dstfd < 0)
  40.         return 3;
  41.     if (ioctl(dstfd, BML_UNLOCK_ALL, 0))
  42.         return 4;
  43.     do {
  44.         total_read += bytes_read = read(srcfd, buf, 4096);
  45.         if (!bytes_read)
  46.             break;
  47.         if (bytes_read < 4096)
  48.             memset(&buf[bytes_read], 0, 4096 - bytes_read);
  49.         if (write(dstfd, buf, 4096) < 4096)
  50.             return 5;
  51.     } while(bytes_read == 4096);
  52.    
  53.     close(dstfd);
  54.     close(srcfd);
  55.    
  56.     return 0;
  57. }
  58.  
  59. #ifndef BOARD_BOOT_BML
  60. #define BOARD_BOOT_BML  "dev/block/bml7"
  61. #endif
  62. #ifndef BOARD_RECOVERY_BML
  63. #define BOARD_RECOVERY_BML  "dev/block/bml8"
  64. #endif
  65.  
  66. int cmd_bml_restore_raw_partition(const char *partition, const char *filename)
  67. {
  68.     if (strcmp(partition, "boot") != 0 && strcmp(partition, "recovery") != 0 && strcmp(partition, "recoveryonly") != 0)
  69.         return 6;
  70.  
  71.     int ret = -1;
  72.     if (strcmp(partition, "recoveryonly") != 0) {
  73.         // always restore boot, regardless of whether recovery or boot is flashed.
  74.         // this is because boot and recovery are the same on some samsung phones.
  75.         // unless of course, recoveryonly is explictly chosen (bml8)
  76.         ret = restore_internal("BOARD_BOOT_BML", filename);
  77.         if (ret != 0)
  78.             return ret;
  79.     }
  80.  
  81.     if (strcmp(partition, "recovery") == 0 || strcmp(partition, "recoveryonly") == 0)
  82.         ret = restore_internal("BOARD_RECOVERY_BML", filename);
  83.     return ret;
  84. }
  85.  
  86. int cmd_bml_backup_raw_partition(const char *partition, const char *out_file)
  87. {
  88.     char* bml;
  89.     if (strcmp("boot", partition) == 0)
  90.         bml = "BOARD_BOOT_BML";
  91.     else if (strcmp("recovery", partition) == 0)
  92.         bml = "BOARD_RECOVERY_BML";
  93.     else {
  94.         printf("Invalid partition.\n");
  95.         return -1;
  96.     }
  97.  
  98.     int ch;
  99.     FILE *in;
  100.     FILE *out;
  101.     int val = 0;
  102.     char buf[512];
  103.     unsigned sz = 0;
  104.     unsigned i;
  105.     int ret = -1;
  106.     char *in_file = bml;
  107.  
  108.     in  = fopen ( in_file,  "r" );
  109.     if (in == NULL)
  110.         goto ERROR3;
  111.  
  112.     out = fopen ( out_file,  "w" );
  113.     if (out == NULL)
  114.         goto ERROR2;
  115.  
  116.     fseek(in, 0L, SEEK_END);
  117.     sz = ftell(in);
  118.     fseek(in, 0L, SEEK_SET);
  119.  
  120.     if (sz % 512)
  121.     {
  122.         while ( ( ch = fgetc ( in ) ) != EOF )
  123.             fputc ( ch, out );
  124.     }
  125.     else
  126.     {
  127.         for (i=0; i< (sz/512); i++)
  128.         {
  129.             if ((fread(buf, 512, 1, in)) != 1)
  130.                 goto ERROR1;
  131.             if ((fwrite(buf, 512, 1, out)) != 1)
  132.                 goto ERROR1;
  133.         }
  134.     }
  135.  
  136.     fsync(out);
  137.     ret = 0;
  138. ERROR1:
  139.     fclose ( out );
  140. ERROR2:
  141.     fclose ( in );
  142. ERROR3:
  143.     return ret;
  144. }
  145.  
  146. int cmd_bml_erase_raw_partition(const char *partition)
  147. {
  148.     // TODO: implement raw wipe
  149.     return 0;
  150. }
  151.  
  152. int cmd_bml_erase_partition(const char *partition, const char *filesystem)
  153. {
  154.     return -1;
  155. }
  156.  
  157. int cmd_bml_mount_partition(const char *partition, const char *mount_point, const char *filesystem, int read_only)
  158. {
  159.     return -1;
  160. }
  161.  
  162. int cmd_bml_get_partition_device(const char *partition, char *device)
  163. {
  164.     return -1;
  165. }
Advertisement
Add Comment
Please, Sign In to add comment