Subversion Repositories HelenOS

Rev

Rev 3530 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * Copyright (c) 2008 Jakub Jermar
  3.  * Copyright (c) 2008 Martin Decky
  4.  * All rights reserved.
  5.  *
  6.  * Redistribution and use in source and binary forms, with or without
  7.  * modification, are permitted provided that the following conditions
  8.  * are met:
  9.  *
  10.  * - Redistributions of source code must retain the above copyright
  11.  *   notice, this list of conditions and the following disclaimer.
  12.  * - Redistributions in binary form must reproduce the above copyright
  13.  *   notice, this list of conditions and the following disclaimer in the
  14.  *   documentation and/or other materials provided with the distribution.
  15.  * - The name of the author may not be used to endorse or promote products
  16.  *   derived from this software without specific prior written permission.
  17.  *
  18.  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  19.  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  20.  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  21.  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  22.  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  23.  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  24.  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  25.  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  27.  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28.  */
  29.  
  30. /** @addtogroup libblock
  31.  * @{
  32.  */
  33. /**
  34.  * @file
  35.  * @brief
  36.  */
  37.  
  38. #include "libblock.h"
  39. #include "../../srv/vfs/vfs.h"
  40. #include "../../srv/rd/rd.h"
  41. #include <errno.h>
  42. #include <async.h>
  43. #include <ipc/ipc.h>
  44. #include <as.h>
  45. #include <assert.h>
  46.  
  47. /** Read data from a block device.
  48.  *
  49.  * @param phone     Phone to be used to communicate with the device.
  50.  * @param buffer    Communication buffer shared with the device.
  51.  * @param bufpos    Pointer to the first unread valid offset within the
  52.  *          communication buffer.
  53.  * @param buflen    Pointer to the number of unread bytes that are ready in
  54.  *          the communication buffer.
  55.  * @param pos       Device position to be read.
  56.  * @param dst       Destination buffer.
  57.  * @param size      Size of the destination buffer.
  58.  * @param block_size    Block size to be used for the transfer.
  59.  *
  60.  * @return      True on success, false on failure.
  61.  */
  62. bool blockread(int phone, void *buffer, off_t *bufpos, size_t *buflen,
  63.     off_t *pos, void *dst, size_t size, size_t block_size)
  64. {
  65.     off_t offset = 0;
  66.     size_t left = size;
  67.    
  68.     while (left > 0) {
  69.         size_t rd;
  70.        
  71.         if (*bufpos + left < *buflen)
  72.             rd = left;
  73.         else
  74.             rd = *buflen - *bufpos;
  75.        
  76.         if (rd > 0) {
  77.             /*
  78.              * Copy the contents of the communication buffer to the
  79.              * destination buffer.
  80.              */
  81.             memcpy(dst + offset, buffer + *bufpos, rd);
  82.             offset += rd;
  83.             *bufpos += rd;
  84.             *pos += rd;
  85.             left -= rd;
  86.         }
  87.        
  88.         if (*bufpos == *buflen) {
  89.             /* Refill the communication buffer with a new block. */
  90.             ipcarg_t retval;
  91.             int rc = async_req_2_1(phone, RD_READ_BLOCK,
  92.                 *pos / block_size, block_size, &retval);
  93.             if ((rc != EOK) || (retval != EOK))
  94.                 return false;
  95.            
  96.             *bufpos = 0;
  97.             *buflen = block_size;
  98.         }
  99.     }
  100.    
  101.     return true;
  102. }
  103.  
  104. int dev_phone = -1;     /* FIXME */
  105. void *dev_buffer = NULL;    /* FIXME */
  106.  
  107. block_t *block_get(dev_handle_t dev_handle, off_t offset, size_t bs)
  108. {
  109.     /* FIXME */
  110.     block_t *b;
  111.     off_t bufpos = 0;
  112.     size_t buflen = 0;
  113.     off_t pos = offset * bs;
  114.  
  115.     assert(dev_phone != -1);
  116.     assert(dev_buffer);
  117.  
  118.     b = malloc(sizeof(block_t));
  119.     if (!b)
  120.         return NULL;
  121.    
  122.     b->data = malloc(bs);
  123.     if (!b->data) {
  124.         free(b);
  125.         return NULL;
  126.     }
  127.     b->size = bs;
  128.  
  129.     if (!blockread(dev_phone, dev_buffer, &bufpos, &buflen, &pos, b->data,
  130.         bs, bs)) {
  131.         free(b->data);
  132.         free(b);
  133.         return NULL;
  134.     }
  135.  
  136.     return b;
  137. }
  138.  
  139. void block_put(block_t *block)
  140. {
  141.     /* FIXME */
  142.     free(block->data);
  143.     free(block);
  144. }
  145.  
  146. /** @}
  147.  */
  148.