📄 ioctl-example.c
字号:
disks[ fp3.noOfBDTLPartitions ].length = (50 * 0x100000); /* 5-th disk size in bytes */ disks[ fp3.noOfBDTLPartitions++ ].noOfSpareUnits = 3; disks[ fp3.noOfBDTLPartitions ].length = (60 * 0x100000); /* 6-th disk size in bytes */ disks[ fp3.noOfBDTLPartitions++ ].noOfSpareUnits = 3; disks[ fp3.noOfBDTLPartitions ].length = (70 * 0x100000); /* 7-th disk size in bytes */ disks[ fp3.noOfBDTLPartitions++ ].noOfSpareUnits = 3; disks[ fp3.noOfBDTLPartitions ].length = (80 * 0x100000); /* 8-th disk size in bytes */ disks[ fp3.noOfBDTLPartitions++ ].noOfSpareUnits = 3; disks[ fp3.noOfBDTLPartitions ].length = (90 * 0x100000); /* 9-th disk size in bytes */ disks[ fp3.noOfBDTLPartitions++ ].noOfSpareUnits = 3; disks[ fp3.noOfBDTLPartitions ].length = (50 * 0x100000); /* 10-th disk size in bytes */ disks[ fp3.noOfBDTLPartitions++ ].noOfSpareUnits = 3; disks[ fp3.noOfBDTLPartitions ].length = (50 * 0x100000); /* 11-th disk size in bytes */ disks[ fp3.noOfBDTLPartitions++ ].noOfSpareUnits = 3; disks[ fp3.noOfBDTLPartitions ].length = (50 * 0x100000); /* 12-th disk size in bytes */ disks[ fp3.noOfBDTLPartitions++ ].noOfSpareUnits = 3; disks[ fp3.noOfBDTLPartitions ].length = (50 * 0x100000); /* 13-th disk size in bytes */ disks[ fp3.noOfBDTLPartitions++ ].noOfSpareUnits = 3;#endif#if 1 /* specify size of last DiskOnChip disk (if you need it) */ disks[ fp3.noOfBDTLPartitions ].length = 0; /* take all remaing space on DiskOnChip */ disks[ fp3.noOfBDTLPartitions++ ].noOfSpareUnits = 3;#endif#if 0 /* specify size of the first binary partition */ bins[fp3.noOfBinaryPartitions].length = (1 * 0x100000); /* size in bytes */ memcpy (bins[fp3.noOfBinaryPartitions].sign, "BIPO", 4); fp3.noOfBinaryPartitions++; /* specify size of the second binary partition */ bins[fp3.noOfBinaryPartitions].length = (2 * 0x100000); /* size in bytes */ memcpy (bins[fp3.noOfBinaryPartitions].sign, "BIPO", 4); fp3.noOfBinaryPartitions++;#endif ioctl_data.inputRecord = ∈ ioctl_data.outputRecord = &out; rc = ioctl (fd, FL_IOCTL_FLASH_FORMAT, &ioctl_data); if ((rc != 0) || (out.status != flOK)) fprintf (stderr, "FL_IOCTL_FLASH_FORMAT failed %d\n", out.status); } /* re-mount disk */ { flMountInput in; in.type = FL_MOUNT; ioctl_data.inputRecord = ∈ ioctl_data.outputRecord = &out; rc = ioctl (fd, FL_IOCTL_MOUNT_VOLUME, &ioctl_data); if ((rc != 0) || (out.status != flOK)) fprintf (stderr, "FL_IOCTL_MOUNT_VOLUME/FL_MOUNT failed %d\n", out.status); } /* WARNING: You must kill DiskOnChip driver and reboot your system now ! */ return rc;}#endif /* ALLOW_FORMATTING */#ifdef ALLOW_FORMATTINGstaticint fl_ioctl_flash_unformat (int fd){ flIOctlRecord ioctl_data; flOutputStatusRecord out; int rc; /* unmount disk before formatting */ { flMountInput in; in.type = FL_DISMOUNT; ioctl_data.inputRecord = ∈ ioctl_data.outputRecord = &out; rc = ioctl (fd, FL_IOCTL_MOUNT_VOLUME, &ioctl_data); if ((rc != 0) || (out.status != flOK)) fprintf (stderr, "FL_IOCTL_MOUNT_VOLUME/FL_DISMOUNT failed %d\n", out.status); } { flFlashUnformatInput in; in.dwFlags = 0; ioctl_data.inputRecord = ∈ ioctl_data.outputRecord = &out; rc = ioctl (fd, FL_IOCTL_FLASH_UNFORMAT, &ioctl_data); if ((rc != 0) || (out.status != flOK)) fprintf (stderr, "FL_IOCTL_FLASH_UNFORMAT failed %d\n", out.status); } /* WARNING: You must kill DiskOnChip driver and reboot your system now ! */ return rc;}#endif /* ALLOW_FORMATTING */staticint hdio_getgeo (int fd){ struct hd_geometry { unsigned char heads; unsigned char sectors; unsigned short cylinders; unsigned long start; }; struct hd_geometry geo; int rc; if ((rc = ioctl(fd, 0x0301 /* HDIO_GETGEO */, &geo)) != 0) perror ("Error in HDIO_GETGEO"); else printf ("Disk geometry: %d heads, %d cylinders, %d sectors, start sector %ld\n", (int)geo.heads, (int)geo.cylinders, (int)geo.sectors, geo.start); return rc;}#ifdef ALLOW_BDKstaticint fl_ioctl_bdk_operation (int fd){ flIOctlRecord ioctl_data; int buf_size = (4 * 1024); char * buf; char * buf2; int rc; register int iBin = 0; /* allocate buffers */ if ((buf = malloc(buf_size)) == NULL) { fprintf (stderr, "Can't alloc buffer"); return -1; } if ((buf2 = malloc(buf_size)) == NULL) { fprintf (stderr, "Can't alloc buffer"); free (buf); return -1; }#if 0 for (; iBin <= 1; iBin++)#endif { /* set this binary partition as the target of subsequent IOCTLs */ { flInputLnxRecord in; flOutputLnxRecord out; in.command = 0; in.data = mk_tffs_handle (0, 0); ioctl_data.inputRecord = ∈ ioctl_data.outputRecord = &out; rc = ioctl (fd, FL_IOCTL_LNX, &ioctl_data); if ((rc != 0) || (out.status != flOK)) { fprintf (stderr, "FL_IOCTL_LNX failed %d\n", (int)out.status); rc = -1; goto quit; } } /* write, the read back and verify 'buf_size' bytes to this binary partition */ { flBDKOperationInput in; flOutputStatusRecord out; fprintf (stdout, "Writing %d bytes to binary partition %d\n", buf_size, iBin); in.type = BDK_GET_INFO; in.bdkStruct.flags = 0; in.bdkStruct.length = 0; in.bdkStruct.startingBlock = 0; in.bdkStruct.bdkBuffer = NULL; in.bdkStruct.signOffset = 8; if (iBin == 0) memcpy (in.bdkStruct.oldSign, "BIPO", 4); else memcpy (in.bdkStruct.oldSign, "BIPO", 4); ioctl_data.inputRecord = ∈ ioctl_data.outputRecord = &out; rc = ioctl (fd, FL_IOCTL_BDK_OPERATION, &ioctl_data); if ((rc != 0) || (out.status != flOK)) { fprintf (stderr, "FL_IOCTL_BDK_OPERATION/BDK_GET_INFO failed %d\n", out.status); rc = -1; goto quit; } in.type = BDK_INIT_WRITE; in.bdkStruct.flags = 0; in.bdkStruct.length = buf_size; in.bdkStruct.bdkBuffer = NULL; in.bdkStruct.signOffset = 8; if (iBin == 0) { in.bdkStruct.startingBlock = 0; memcpy (in.bdkStruct.oldSign, "BIPO", 4); } else { in.bdkStruct.startingBlock = 15; memcpy (in.bdkStruct.oldSign, "BIPO", 4); } ioctl_data.inputRecord = ∈ ioctl_data.outputRecord = &out; rc = ioctl (fd, FL_IOCTL_BDK_OPERATION, &ioctl_data); if ((rc != 0) || (out.status != flOK)) { fprintf (stderr, "FL_IOCTL_BDK_OPERATION/BDK_INIT_WRITE failed %d\n", out.status); rc = -1; goto quit; } in.type = BDK_WRITE; in.bdkStruct.flags = ERASE_BEFORE_WRITE; in.bdkStruct.length = buf_size; in.bdkStruct.bdkBuffer = buf; memset (buf, '$', buf_size); ioctl_data.inputRecord = ∈ ioctl_data.outputRecord = &out; rc = ioctl (fd, FL_IOCTL_BDK_OPERATION, &ioctl_data); if ((rc != 0) || (out.status != flOK)) { fprintf (stderr, "FL_IOCTL_BDK_OPERATION/BDK_WRITE failed %d\n", out.status); rc = -1; goto quit; } fprintf (stdout, "SUCCESS writing %d bytes to binary partition %d\n", buf_size, iBin); fprintf (stdout, "Reading back and verifying %d bytes from binary partition %d\n", buf_size, iBin); in.type = BDK_INIT_READ; ioctl_data.inputRecord = ∈ ioctl_data.outputRecord = &out; rc = ioctl (fd, FL_IOCTL_BDK_OPERATION, &ioctl_data); if ((rc != 0) || (out.status != flOK)) { fprintf (stderr, "FL_IOCTL_BDK_OPERATION/BDK_INIT_READ failed %d\n", out.status); rc = -1; goto quit; } in.type = BDK_READ; in.bdkStruct.bdkBuffer = buf2; memset (buf2, 0, buf_size); ioctl_data.inputRecord = ∈ ioctl_data.outputRecord = &out; rc = ioctl (fd, FL_IOCTL_BDK_OPERATION, &ioctl_data); if ((rc != 0) || (out.status != flOK)) { fprintf (stderr, "FL_IOCTL_BDK_OPERATION/BDK_READ failed %d\n", out.status); rc = -1; goto quit; } if( memcmp(buf, buf2, buf_size) != 0) { fprintf (stderr, "Error: mismatching data\n"); rc = -1; goto quit; } fprintf (stdout, "SUCCESS reading back and verifying data\n"); } } /* for(iBin) */quit: free (buf); free (buf2); return rc;}#endif /* ALLOW_BDK */#ifdef ALLOW_HW_PROTECTION/* * This routine sets TrueFFS handle for subsequent IOCTLs */staticint set_ioctl_handle (int fd, int tffs_handle){ flIOctlRecord ioctl_data; flInputLnxRecord in; flOutputLnxRecord out; int rc; ioctl_data.inputRecord = ∈ ioctl_data.outputRecord = &out; in.command = 0; in.data = tffs_handle; rc = ioctl (fd, FL_IOCTL_LNX, &ioctl_data); if ((rc != 0) || (out.status != flOK)) { fprintf (stderr, "FL_IOCTL_LNX failed %d\n", (int)out.status); rc = -1; } else fprintf (stdout, "SUCCESS setting handle 0x%x for future IOCTLs\n", tffs_handle); return rc;}/* * This routine provides example of using FL_IOCTL_..._HW_PROTECTION * IOCTLs to insert key into protected partition. */staticint fl_ioctl_hw_protection (int fd, int ioctl_opcode){ flIOctlRecord ioctl_data; int socket_no = 0; int partition_no; int rc; partition_no = /* specify your own partition here */ 0; /* set TrueFFS handle for subsequent IOCTL */ rc = set_ioctl_handle (fd, mk_tffs_handle(socket_no, partition_no)); if (rc != 0) return rc; /* insert key into partition */ { flProtectionInput in; flProtectionOutput out; ioctl_data.inputRecord = ∈ ioctl_data.outputRecord = &out; in.type = PROTECTION_INSERT_KEY; /* this is my usual key; replace it with your own */ in.key[0] = 'p'; in.key[1] = 'i'; in.key[2] = 's'; in.key[3] = 's'; in.key[4] = 'w'; in.key[5] = 'o'; in.key[6] = 'r'; in.key[7] = 'd'; in.protectionType = 0; /* not actually used by PROTECTION_INSERT_KEY */ rc = ioctl (fd, ioctl_opcode, &ioctl_data); if ((rc != 0) || (out.status != flOK)) fprintf (stderr, "%d IOCTL failed %d\n", ioctl_opcode, out.status); else fprintf (stdout, "SUCCESS inserting protection key\n"); } /* for security, set TrueFFS handle to impossible value (0xffff) */ set_ioctl_handle (fd, 0xffff); return rc;}#endif /* ALLOW_HW_PROTECTION */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -