tz_fileio.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #include <linux/fs.h>
  2. #include <asm/segment.h>
  3. #include <linux/uaccess.h>
  4. #include <linux/buffer_head.h>
  5. #include "tz_fileio.h"
  6. /** FILE_Open
  7. * Open a file for read or write
  8. *
  9. * @param path File path to open
  10. * @param flags Access flag see open(2)
  11. * @param mode The mode/permission for created file, see open(2)
  12. * @retval File handle
  13. */
  14. struct file *FILE_Open(const char *path, int flags, int mode)
  15. {
  16. struct file *filp = NULL;
  17. mm_segment_t oldfs;
  18. int err = 0;
  19. oldfs = get_fs();
  20. set_fs(get_ds());
  21. filp = filp_open(path, flags, mode);
  22. set_fs(oldfs);
  23. if (IS_ERR(filp)) {
  24. err = PTR_ERR(filp);
  25. return NULL;
  26. }
  27. return filp;
  28. }
  29. /** FILE_Read
  30. * Read data from a file.
  31. *
  32. * @param file Opened file handle
  33. * @param offset Pointer to begin offset to read.
  34. * @param data user's buffer address.
  35. * @param size readdata length in bytes.
  36. * @retval >0 SUCCESS, return actual bytes read.
  37. * @retval <0 Fail, errno
  38. */
  39. int FILE_Read(struct file *file, unsigned char *data, unsigned int size,
  40. unsigned long long *offset)
  41. {
  42. mm_segment_t oldfs;
  43. int ret;
  44. oldfs = get_fs();
  45. set_fs(get_ds());
  46. ret = vfs_read(file, data, size, offset);
  47. set_fs(oldfs);
  48. return ret;
  49. }
  50. /** FILE_Write
  51. * Write data from a file.
  52. *
  53. * @param file Opened file handle
  54. * @param offset Pointer to begin offset to Write.
  55. * @param data user's buffer address.
  56. * @param size Writedata length in bytes.
  57. * @retval >0 SUCCESS, return actual bytes Write.
  58. * @retval <0 Fail, errno
  59. */
  60. int FILE_Write(struct file *file, unsigned char *data, unsigned int size,
  61. unsigned long long *offset)
  62. {
  63. mm_segment_t oldfs;
  64. int ret;
  65. oldfs = get_fs();
  66. set_fs(get_ds());
  67. ret = vfs_write(file, data, size, offset);
  68. set_fs(oldfs);
  69. return ret;
  70. }
  71. /** FILE_ReadData
  72. * Read data from a file.
  73. *
  74. * @param path File path name to read.
  75. * @param u4Offset begin offset to read.
  76. * @param pData user's buffer address.
  77. * @param i4Length readdata length in bytes.
  78. * @retval >0 SUCCESS, return actual bytes read.
  79. * @retval <0 Fail, errno
  80. */
  81. int FILE_ReadData(const char *path, unsigned int u4Offset, char *pData,
  82. int i4Length)
  83. {
  84. struct file *file = NULL;
  85. UINT64 u8Offset = (UINT64)u4Offset;
  86. file = FILE_Open(path, O_RDONLY, 0);
  87. if (!file)
  88. return -EFAULT;
  89. i4Length = FILE_Read(file, (void *)pData, i4Length, &u8Offset);
  90. filp_close(file, NULL);
  91. return i4Length;
  92. }
  93. /** FILE_WriteData
  94. * Write data to a file.
  95. *
  96. * @param path File path name to write.
  97. * @param u4Offset begin offset to write.
  98. * @param pData user's buffer address.
  99. * @param i4Length writedata length in bytes.
  100. * @retval >0 SUCCESS, return actual bytes writen.
  101. * @retval <0 Fail, errno
  102. */
  103. int FILE_WriteData(const char *path, unsigned int u4Offset, char *pData,
  104. int i4Length)
  105. {
  106. struct file *file = NULL;
  107. UINT64 u8Offset = (UINT64)u4Offset;
  108. file = FILE_Open(path, O_WRONLY|O_CREAT, 0644);
  109. if (!file)
  110. return -EFAULT;
  111. i4Length = FILE_Write(file, (void *)pData, i4Length, &u8Offset);
  112. filp_close(file, NULL);
  113. return i4Length;
  114. }