You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ramdisk.c 2.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. FILE_LICENCE ( GPL2_OR_LATER );
  19. #include <gpxe/blockdev.h>
  20. #include <gpxe/ramdisk.h>
  21. /**
  22. * @file
  23. *
  24. * RAM disks
  25. *
  26. */
  27. static inline __attribute__ (( always_inline )) struct ramdisk *
  28. block_to_ramdisk ( struct block_device *blockdev ) {
  29. return container_of ( blockdev, struct ramdisk, blockdev );
  30. }
  31. /**
  32. * Read block
  33. *
  34. * @v blockdev Block device
  35. * @v block Block number
  36. * @v count Block count
  37. * @v buffer Data buffer
  38. * @ret rc Return status code
  39. */
  40. static int ramdisk_read ( struct block_device *blockdev, uint64_t block,
  41. unsigned long count, userptr_t buffer ) {
  42. struct ramdisk *ramdisk = block_to_ramdisk ( blockdev );
  43. unsigned long offset = ( block * blockdev->blksize );
  44. unsigned long length = ( count * blockdev->blksize );
  45. DBGC ( ramdisk, "RAMDISK %p reading [%lx,%lx)\n",
  46. ramdisk, offset, length );
  47. memcpy_user ( buffer, 0, ramdisk->data, offset, length );
  48. return 0;
  49. }
  50. /**
  51. * Write block
  52. *
  53. * @v blockdev Block device
  54. * @v block Block number
  55. * @v count Block count
  56. * @v buffer Data buffer
  57. * @ret rc Return status code
  58. */
  59. static int ramdisk_write ( struct block_device *blockdev, uint64_t block,
  60. unsigned long count, userptr_t buffer ) {
  61. struct ramdisk *ramdisk = block_to_ramdisk ( blockdev );
  62. unsigned long offset = ( block * blockdev->blksize );
  63. unsigned long length = ( count * blockdev->blksize );
  64. DBGC ( ramdisk, "RAMDISK %p writing [%lx,%lx)\n",
  65. ramdisk, offset, length );
  66. memcpy_user ( ramdisk->data, offset, buffer, 0, length );
  67. return 0;
  68. }
  69. static struct block_device_operations ramdisk_operations = {
  70. .read = ramdisk_read,
  71. .write = ramdisk_write
  72. };
  73. int init_ramdisk ( struct ramdisk *ramdisk, userptr_t data, size_t len,
  74. unsigned int blksize ) {
  75. if ( ! blksize )
  76. blksize = 512;
  77. ramdisk->data = data;
  78. ramdisk->blockdev.op = &ramdisk_operations;
  79. ramdisk->blockdev.blksize = blksize;
  80. ramdisk->blockdev.blocks = ( len / blksize );
  81. return 0;
  82. }