Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Sample code to access our char device */
- #include <stdio.h>
- #include <unistd.h>
- #include <linux/types.h>
- #include <sys/ioctl.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- /* Greybus S2 Loader Load method */
- #define GB_S2L_LOAD_METHOD_BOU 0x01
- #define GB_S2L_LOAD_METHOD_FLASH 0x02
- /* Greybus S2 Loader interface status */
- #define GB_S2L_INTERFACE_STATUS_INVALID 0x00 /* Firmware package could not be validated */
- #define GB_S2L_INTERFACE_STATUS_INSECURE 0x01 /* Firmware package is valid but insecure */
- #define GB_S2L_INTERFACE_STATUS_SECURE 0x02 /* Firmware package is valid and secure */
- /* Greybus S2 Loader authorize to boot status */
- #define GB_S2L_ATB_STATUS_INVALID 0x00
- #define GB_S2L_ATB_STATUS_DENIED 0x01
- #define GB_S2L_ATB_STATUS_ALLOWED 0x02
- /* IOCTL support */
- #define S2L_IOCTL_BASE 'S'
- struct s2l_load_s3_info {
- __u16 version_major;
- __u16 version_minor;
- __u8 load_method; /* Should be set with: GB_S2L_LOAD_METHOD_XXX */
- __u8 status; /* Should be set with: GB_S2L_INTERFACE_STATUS_XXX */
- };
- #define S2LIOC_FLASH_AVAILABLE _IOR(S2L_IOCTL_BASE, 0, __u8)
- #define S2LIOC_LOAD_S3 _IOWR(S2L_IOCTL_BASE, 1, struct s2l_load_s3_info)
- #define S2LIOC_AUTHORIZE_TO_BOOT _IOW(S2L_IOCTL_BASE, 2, __u8)
- const char *cdev = "/dev/char/251:0";
- const char buf[] = "Please verify yourself";
- char rxbuf[4096];
- int main()
- {
- int fd, ret;
- __u8 flash = 0, status;
- char buff[80]="";
- struct s2l_load_s3_info s3_info = {
- .version_major = 0,
- .version_minor = 0,
- };
- fd=open(cdev, O_RDWR);
- if (!fd) {
- printf("Failed to open char-dev: %s\n", cdev);
- return -1;
- }
- printf("Opened char-dev: %s\n", cdev);
- /* Authenticate module */
- printf("Authentication request: %s\n", buf);
- ret = write(fd, buf, sizeof(buf));
- if (ret <= 0) {
- printf("Failed to write to char-dev: %s (%d)\n", cdev, ret);
- return -1;
- }
- ret = read(fd, rxbuf, sizeof(rxbuf));
- if (ret <= 0) {
- printf("Failed to read from char-dev: %s (%d)\n", cdev, ret);
- return -1;
- }
- printf("Authentication response: %s\n", rxbuf);
- ret = ioctl(fd, S2LIOC_FLASH_AVAILABLE, &flash);
- if (ret < 0) {
- printf("Failed to get flash-available: %s (%d)\n", cdev, ret);
- return -1;
- }
- printf("S2L Flash Available: %d\n", flash);
- /* Try boot over unipro first */
- s3_info.load_method = GB_S2L_LOAD_METHOD_BOU;
- ret = ioctl(fd, S2LIOC_LOAD_S3, &s3_info);
- if (ret < 0) {
- printf("Failed to load over unipro: %s (%d)\n", cdev, ret);
- return -1;
- }
- printf("BoU done: major: %d, minor: %d, status: %d\n",
- s3_info.version_major, s3_info.version_minor, s3_info.status);
- if (flash) {
- /* Try boot over flash now */
- s3_info.load_method = GB_S2L_LOAD_METHOD_FLASH;
- ret = ioctl(fd, S2LIOC_LOAD_S3, &s3_info);
- if (ret < 0) {
- printf("Failed to load from flash: %s (%d)\n", cdev, ret);
- return -1;
- }
- printf("Flash load done: major: %d, minor: %d, status: %d\n",
- s3_info.version_major, s3_info.version_minor,
- s3_info.status);
- }
- /* Authenticate module */
- status = GB_S2L_ATB_STATUS_ALLOWED;
- ret = ioctl(fd, S2LIOC_AUTHORIZE_TO_BOOT, &status);
- if (ret < 0) {
- printf("Failed to authorize module to boot: %s (%d)\n", cdev, ret);
- return -1;
- }
- printf("S2L Authorized module\n");
- close(fd);
- }
Add Comment
Please, Sign In to add comment