vireshk

S2 Loader app

Feb 18th, 2016
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.24 KB | None | 0 0
  1. /* Sample code to access our char device */
  2.  
  3. #include <stdio.h>
  4. #include <unistd.h>
  5. #include <linux/types.h>
  6. #include <sys/ioctl.h>
  7. #include <sys/stat.h>
  8. #include <fcntl.h>
  9.  
  10. /* Greybus S2 Loader Load method */
  11. #define GB_S2L_LOAD_METHOD_BOU 0x01
  12. #define GB_S2L_LOAD_METHOD_FLASH 0x02
  13.  
  14. /* Greybus S2 Loader interface status */
  15. #define GB_S2L_INTERFACE_STATUS_INVALID 0x00 /* Firmware package could not be validated */
  16. #define GB_S2L_INTERFACE_STATUS_INSECURE 0x01 /* Firmware package is valid but insecure */
  17. #define GB_S2L_INTERFACE_STATUS_SECURE 0x02 /* Firmware package is valid and secure */
  18.  
  19. /* Greybus S2 Loader authorize to boot status */
  20. #define GB_S2L_ATB_STATUS_INVALID 0x00
  21. #define GB_S2L_ATB_STATUS_DENIED 0x01
  22. #define GB_S2L_ATB_STATUS_ALLOWED 0x02
  23.  
  24. /* IOCTL support */
  25. #define S2L_IOCTL_BASE 'S'
  26.  
  27. struct s2l_load_s3_info {
  28. __u16 version_major;
  29. __u16 version_minor;
  30. __u8 load_method; /* Should be set with: GB_S2L_LOAD_METHOD_XXX */
  31. __u8 status; /* Should be set with: GB_S2L_INTERFACE_STATUS_XXX */
  32. };
  33.  
  34. #define S2LIOC_FLASH_AVAILABLE _IOR(S2L_IOCTL_BASE, 0, __u8)
  35. #define S2LIOC_LOAD_S3 _IOWR(S2L_IOCTL_BASE, 1, struct s2l_load_s3_info)
  36. #define S2LIOC_AUTHORIZE_TO_BOOT _IOW(S2L_IOCTL_BASE, 2, __u8)
  37.  
  38. const char *cdev = "/dev/char/251:0";
  39. const char buf[] = "Please verify yourself";
  40. char rxbuf[4096];
  41.  
  42. int main()
  43. {
  44. int fd, ret;
  45. __u8 flash = 0, status;
  46. char buff[80]="";
  47. struct s2l_load_s3_info s3_info = {
  48. .version_major = 0,
  49. .version_minor = 0,
  50. };
  51.  
  52. fd=open(cdev, O_RDWR);
  53. if (!fd) {
  54. printf("Failed to open char-dev: %s\n", cdev);
  55. return -1;
  56. }
  57.  
  58. printf("Opened char-dev: %s\n", cdev);
  59.  
  60. /* Authenticate module */
  61. printf("Authentication request: %s\n", buf);
  62.  
  63. ret = write(fd, buf, sizeof(buf));
  64. if (ret <= 0) {
  65. printf("Failed to write to char-dev: %s (%d)\n", cdev, ret);
  66. return -1;
  67. }
  68.  
  69. ret = read(fd, rxbuf, sizeof(rxbuf));
  70. if (ret <= 0) {
  71. printf("Failed to read from char-dev: %s (%d)\n", cdev, ret);
  72. return -1;
  73. }
  74. printf("Authentication response: %s\n", rxbuf);
  75.  
  76. ret = ioctl(fd, S2LIOC_FLASH_AVAILABLE, &flash);
  77. if (ret < 0) {
  78. printf("Failed to get flash-available: %s (%d)\n", cdev, ret);
  79. return -1;
  80. }
  81. printf("S2L Flash Available: %d\n", flash);
  82.  
  83. /* Try boot over unipro first */
  84. s3_info.load_method = GB_S2L_LOAD_METHOD_BOU;
  85. ret = ioctl(fd, S2LIOC_LOAD_S3, &s3_info);
  86. if (ret < 0) {
  87. printf("Failed to load over unipro: %s (%d)\n", cdev, ret);
  88. return -1;
  89. }
  90. printf("BoU done: major: %d, minor: %d, status: %d\n",
  91. s3_info.version_major, s3_info.version_minor, s3_info.status);
  92.  
  93. if (flash) {
  94. /* Try boot over flash now */
  95. s3_info.load_method = GB_S2L_LOAD_METHOD_FLASH;
  96. ret = ioctl(fd, S2LIOC_LOAD_S3, &s3_info);
  97. if (ret < 0) {
  98. printf("Failed to load from flash: %s (%d)\n", cdev, ret);
  99. return -1;
  100. }
  101. printf("Flash load done: major: %d, minor: %d, status: %d\n",
  102. s3_info.version_major, s3_info.version_minor,
  103. s3_info.status);
  104. }
  105.  
  106. /* Authenticate module */
  107. status = GB_S2L_ATB_STATUS_ALLOWED;
  108. ret = ioctl(fd, S2LIOC_AUTHORIZE_TO_BOOT, &status);
  109. if (ret < 0) {
  110. printf("Failed to authorize module to boot: %s (%d)\n", cdev, ret);
  111. return -1;
  112. }
  113. printf("S2L Authorized module\n");
  114.  
  115. close(fd);
  116. }
Add Comment
Please, Sign In to add comment