Advertisement
Guest User

problem reading @ line 106. hielfe?

a guest
May 20th, 2018
314
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.17 KB | None | 0 0
  1. /* Dtpass - Driver to access Switch's encrypted partition
  2. major thanks to
  3. https://www.apriorit.com/dev-blog/195-simple-driver-for-linux-os
  4. and
  5. https://linux-kernel-labs.github.io/master/labs/block_device_drivers.html
  6.  
  7. for their sample code & explanation. they explain it so well how it works
  8.  
  9. Copyright (C) 2016-2017 DacoTaco
  10. This program is free software; you can redistribute it and/or
  11. modify it under the terms of the GNU General Public License
  12. as published by the Free Software Foundation version 2.
  13. This program is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. GNU General Public License for more details.
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  20.  
  21. */
  22.  
  23. #include <linux/init.h>
  24. #include <linux/module.h> // module info
  25. #include <linux/syscalls.h> //syscalls!
  26. #include <linux/types.h> //dem types
  27. #include <linux/fs.h> // file related stuff
  28. #include <linux/kernel.h> // printk <3
  29. #include <linux/errno.h> // error codes
  30. #include <linux/genhd.h>
  31. #include <linux/blkdev.h> //block device <3
  32. #include <linux/bio.h> //bio, request data structure
  33. #include <linux/vmalloc.h>
  34. #include <linux/genhd.h>
  35. #include <linux/fcntl.h>
  36. #include <linux/uaccess.h>
  37.  
  38. //encryption stuff
  39. #include "aes.h"
  40. #include "ccrypto.c"
  41. #include "driver.h"
  42.  
  43. #define BISKEY0_CRYPT 0x946105EF7E14EFEA12D37BDCFFA38430
  44. #define BISKEY0_TWEAK 0x680E1FC8236922D082D6D61E77DAD21B
  45. #define FILE_PATH "/home/dacotaco/shofel2/dtpass/mmcblk1p2_dec.bin"
  46.  
  47. static int dev_major = 0;
  48. static int dev_minor = 1;
  49. static int nr_devices = 1;
  50. static const unsigned int partition_size = 0x00400000;
  51.  
  52. #define device_name "dec_mmcblk1p"
  53. #define NAND_SECTOR_SIZE 0x4000
  54. #define NR_SECTORS (partition_size / KERNEL_SECTOR_SIZE)
  55. #define KERNEL_SECTOR_SIZE 512
  56.  
  57. static struct dtpass_dev {
  58. spinlock_t lock; //used for exclusion & LOCKING DEM SHITZ
  59. struct request_queue *queue; // le queue to handle IO stuff
  60. struct gendisk *gd;
  61. char filename[256];
  62. struct file *fp;
  63. unsigned int partition_size;
  64. } dev;
  65.  
  66. //----------------------------------------------
  67. // I/O block device
  68. //----------------------------------------------
  69. static int transfer_data(struct dtpass_dev* dev, size_t start_off, size_t len, char* dst,int direction)
  70. {
  71. int size = len;
  72. mm_segment_t fs_status;
  73. int ret = 0;
  74. loff_t pos = 0;
  75.  
  76. if(size > dev->partition_size)
  77. {
  78. printk(KERN_NOTICE "DTPASS : fixing size\n");
  79. size = dev->partition_size;
  80. }
  81.  
  82. //pos = where to start reading
  83. pos = (loff_t)(start_off * KERNEL_SECTOR_SIZE);
  84.  
  85. printk( KERN_NOTICE "start_off : 0x%lx / 0x%llx ,len : 0x%lx, direction 0x%x, ",
  86. start_off,pos,len,direction);
  87.  
  88. //return;
  89. fs_status = get_fs();
  90. if (direction == READ)
  91. {
  92. unsigned char *buf = (unsigned char*)vmalloc(size+1);//,GFP_KERNEL);
  93. if(buf == NULL)
  94. {
  95. printk(KERN_WARNING "DTPASS : failed to malloc read buf\n");
  96. goto return_func;
  97. }
  98.  
  99. printk( KERN_NOTICE "buf : 0x%x , fd : 0x%x\n",buf,dev->fp);
  100.  
  101. memset(buf,0,size+1);
  102. set_fs(KERNEL_DS);
  103.  
  104. //vfs_llseek(dev->fp,pos,SEEK_SET);
  105. //any read of address 0x1000 or higher results in vfs_read returning -EINTR? :/
  106. ret = vfs_read(dev->fp, buf, size,&pos);
  107.  
  108. if(ret == -EINTR)
  109. {
  110. //retry? not that it fixes anything....
  111. printk(KERN_NOTICE "retrying..\n");
  112. ret = vfs_read(dev->fp, buf, size,&pos);
  113. }
  114.  
  115. if(ret < 0)
  116. printk(KERN_NOTICE "DTPASS : read failure : %d\n",ret);
  117.  
  118. copy_to_user(dst,buf,size);
  119.  
  120. vfree(buf);
  121.  
  122. }
  123. else
  124. {
  125. printk(KERN_NOTICE "DTPASS : write not yet implemented!");
  126. }
  127.  
  128. return_func:
  129. set_fs(fs_status);
  130. return ret;
  131. }
  132. static int process_bio(struct dtpass_dev* dev,struct bio *bio)
  133. {
  134. if(dev == NULL || bio == NULL)
  135. printk(KERN_NOTICE "DTPASS : bio processing called on null data");
  136.  
  137. //process the bio data
  138. struct bio_vec bvec;
  139. struct bvec_iter iter;
  140. int dir = bio_data_dir(bio);
  141. int ret = 0;
  142.  
  143. //loop through each bio segment
  144. //basically this loops trough all bio segments, copies data to the bvec vector and then retrieves data
  145. //from that to initiate the transfer function
  146. printk(KERN_NOTICE "LEGGO\n");
  147. bio_for_each_segment(bvec,bio,iter)
  148. {
  149. //get the data from bio needed to make the transfer
  150. sector_t sector = iter.bi_sector;
  151. char* dst = kmap_atomic(bvec.bv_page);
  152. unsigned long dst_offset = bvec.bv_offset;
  153. size_t len = bvec.bv_len;
  154.  
  155.  
  156. //process the data
  157. ret = transfer_data(dev,sector,len,dst+dst_offset,dir);
  158.  
  159. //lolidk, something about manging memory from userpsace & kernel i think?
  160. kunmap_atomic(dst);
  161. }
  162. printk(KERN_NOTICE "KBYE\n");
  163.  
  164. return ret;
  165. }
  166. static void block_device_request(struct request_queue *queue)
  167. {
  168. //kernel decided its time we handled the device requests. lets handle them shall we?
  169. //also, normally the kernel already upholds the lock, so no need to get the lock
  170.  
  171. struct request *req;
  172. struct dtpass_dev *dev = queue->queuedata;
  173. while(1)
  174. {
  175. req = blk_fetch_request(queue);
  176. if(req == NULL)
  177. {
  178. //we got a null request, aka : we've processed them all
  179. break;
  180. }
  181.  
  182. if(blk_rq_is_passthrough(req))
  183. {
  184. //we got a special command that isn't about transferring data.
  185. //how do we deal with these? lolidk, so meh XD
  186. //set request as processed with the EIO error
  187. printk( KERN_NOTICE "DTPASS : skipping non-fs request\n");
  188. __blk_end_request_all(req, -EIO);
  189. continue;
  190. }
  191.  
  192. process_bio(dev,req->bio);
  193.  
  194. //request done! set it processed with success , 0
  195. __blk_end_request_all(req,0);
  196.  
  197. }
  198.  
  199. return;
  200. }
  201.  
  202. static int open_block_device(struct block_device *bdev,fmode_t mode)
  203. {
  204. return 0;
  205. }
  206.  
  207. static void release_block_device(struct gendisk *gd, fmode_t mode)
  208. {
  209. return;
  210. }
  211.  
  212. struct block_device_operations block_ops = {
  213. .owner = THIS_MODULE,
  214. .open = open_block_device,
  215. .release = release_block_device
  216. };
  217.  
  218. //----------------------------------------------
  219. // block device managing
  220. //----------------------------------------------
  221.  
  222. static int create_block_device(struct dtpass_dev *dev)
  223. {
  224.  
  225. //Init I/O queue
  226. spin_lock_init(&dev->lock); //set lock pointer
  227.  
  228. dev->queue = blk_init_queue(block_device_request, & dev->lock); //set method to handle queue's
  229. if (dev->queue == NULL)
  230. {
  231. goto err;
  232. }
  233.  
  234. blk_queue_logical_block_size(dev->queue, KERNEL_SECTOR_SIZE);
  235. //blk_queue_physical_block_size(dev->queue,NAND_SECTOR_SIZE/KERNEL_SECTOR_SIZE);
  236. dev->queue->queuedata = dev;
  237.  
  238. //init the gd!
  239. dev->gd = alloc_disk(nr_devices);
  240.  
  241. if(!dev->gd)
  242. {
  243. printk( KERN_NOTICE "alloc_disk failure\n");
  244. return -ENOMEM;
  245. }
  246.  
  247. //set filename of file we are using
  248. snprintf(dev->filename,255,FILE_PATH);
  249.  
  250. //open file
  251. mm_segment_t fs_status = get_fs();
  252. dev->fp = filp_open(dev->filename,O_RDONLY,0);
  253. if(IS_ERR(dev->fp))
  254. {
  255. printk(KERN_WARNING "DTPASS : failed to open device\n");
  256. return PTR_ERR(dev->fp);
  257. }
  258. set_fs(fs_status);
  259.  
  260. dev->partition_size = partition_size;
  261. dev->gd->major = dev_major;
  262. dev->gd->first_minor = 0;
  263. dev->gd->fops = &block_ops;
  264. dev->gd->queue = dev->queue;
  265. dev->gd->private_data = dev;
  266. snprintf(dev->gd->disk_name,32,device_name);
  267. set_capacity(dev->gd,NR_SECTORS);
  268.  
  269.  
  270. add_disk(dev->gd);
  271. return 0;
  272.  
  273. err:
  274. return -ENOMEM;
  275.  
  276. }
  277.  
  278. static void delete_block_device(struct dtpass_dev *dev)
  279. {
  280. if(dev == NULL)
  281. return;
  282.  
  283. //close fp
  284. if(dev->fp != NULL)
  285. filp_close(dev->fp, NULL);
  286.  
  287. //clean up device
  288. if(dev->gd)
  289. del_gendisk(dev->gd);
  290.  
  291. //clean up queue
  292. if(dev->queue)
  293. blk_cleanup_queue(dev->queue);
  294.  
  295. }
  296.  
  297. //----------------------------------------------
  298. // registry related sheise
  299. //----------------------------------------------
  300.  
  301.  
  302. static int register_device(void)
  303. {
  304. int ret;
  305.  
  306. //we register our block device
  307. ret = register_blkdev(dev_major,device_name);
  308. if(ret < 0)
  309. {
  310. printk( KERN_WARNING "DTPASS: failed to register block device\n");
  311. return -EBUSY;
  312. }
  313. dev_major = ret;
  314.  
  315. //create the block device
  316. ret = create_block_device(&dev);
  317. if(ret < 0)
  318. return ret;
  319.  
  320.  
  321. return 0;
  322. }
  323.  
  324. static void unregister_device(void)
  325. {
  326. delete_block_device(&dev);
  327. unregister_blkdev(dev_major, device_name);
  328. }
  329.  
  330. //----------------------------------------------
  331. // Module init stuff
  332. //----------------------------------------------
  333.  
  334. static int __init dtpass_init(void)
  335. {
  336. int ret = register_device();
  337.  
  338. if(ret == 0)
  339. {
  340. printk( KERN_NOTICE "DTPASS: module loaded\n" );
  341. }
  342. else
  343. printk( KERN_NOTICE "DTPASS: failed to load module : %u\n",ret);
  344.  
  345.  
  346. return ret;
  347. }
  348.  
  349. static void __exit dtpass_dispose(void)
  350. {
  351. unregister_device();
  352. printk( KERN_NOTICE "DTPASS: module unloaded\n" );
  353. }
  354.  
  355. module_init(dtpass_init);
  356. module_exit(dtpass_dispose);
  357.  
  358. MODULE_LICENSE("GPL");
  359. MODULE_AUTHOR("DacoTaco");
  360. MODULE_DESCRIPTION("linux module to allow decrypted (read) access to the encrypted NAND partitions");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement