Changeset 7858bc5f in mainline for uspace/lib/libblock/libblock.c


Ignore:
Timestamp:
2008-10-28T15:40:19Z (16 years ago)
Author:
Jakub Jermar <jakub@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
916bf1a
Parents:
5cf723b
Message:

Setup communication parameters with the block device in block_init(). The file
system now doesn't know anything about the communication with the block device.
Rename blockread() to block_read(). The boot block is now read only once. The file
system can get access it using the block_bb_get() function.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/libblock/libblock.c

    r5cf723b r7858bc5f  
    3939#include "../../srv/vfs/vfs.h"
    4040#include "../../srv/rd/rd.h"
     41#include <ipc/devmap.h>
     42#include <ipc/services.h>
    4143#include <errno.h>
     44#include <sys/mman.h>
    4245#include <async.h>
    4346#include <ipc/ipc.h>
     
    4548#include <assert.h>
    4649
     50static int dev_phone = -1;              /* FIXME */
     51static void *dev_buffer = NULL;         /* FIXME */
     52static size_t dev_buffer_len = 0;       /* FIXME */
     53static void *bblock = NULL;             /* FIXME */
     54
     55int
     56block_init(dev_handle_t dev_handle, size_t com_size, off_t bb_off,
     57    size_t bb_size)
     58{
     59        int rc;
     60
     61        bblock = malloc(bb_size);
     62        if (!bblock)
     63                return ENOMEM;
     64        dev_buffer_len = com_size;
     65        dev_buffer = mmap(NULL, com_size, PROTO_READ | PROTO_WRITE,
     66            MAP_ANONYMOUS | MAP_PRIVATE, 0, 0);
     67        if (!dev_buffer) {
     68                free(bblock);
     69                return ENOMEM;
     70        }
     71        dev_phone = ipc_connect_me_to(PHONE_NS, SERVICE_DEVMAP,
     72            DEVMAP_CONNECT_TO_DEVICE, dev_handle);
     73
     74        if (dev_phone < 0) {
     75                free(bblock);
     76                munmap(dev_buffer, com_size);
     77                return dev_phone;
     78        }
     79
     80        rc = ipc_share_out_start(dev_phone, dev_buffer,
     81            AS_AREA_READ | AS_AREA_WRITE);
     82        if (rc != EOK) {
     83                ipc_hangup(dev_phone);
     84                free(bblock);
     85                munmap(dev_buffer, com_size);
     86                return rc;
     87        }
     88        off_t bufpos = 0;
     89        size_t buflen = 0;
     90        if (!block_read(dev_handle, &bufpos, &buflen, &bb_off,
     91            bblock, bb_size, bb_size)) {
     92                ipc_hangup(dev_phone);
     93                free(bblock);
     94                munmap(dev_buffer, com_size);
     95                return EIO;     /* XXX real error code */
     96        }
     97        return EOK;
     98}
     99
     100void block_fini(dev_handle_t dev_handle)
     101{
     102        /* XXX */
     103        free(bblock);
     104        munmap(dev_buffer, dev_buffer_len);
     105        ipc_hangup(dev_phone);
     106}
     107
     108void *block_bb_get(dev_handle_t dev_handle)
     109{
     110        /* XXX */
     111        return bblock;
     112}
     113
    47114/** Read data from a block device.
    48115 *
    49  * @param phone         Phone to be used to communicate with the device.
    50  * @param buffer        Communication buffer shared with the device.
     116 * @param dev_handle    Device handle of the block device.
    51117 * @param bufpos        Pointer to the first unread valid offset within the
    52118 *                      communication buffer.
     
    60126 * @return              True on success, false on failure.
    61127 */
    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)
     128bool
     129block_read(int dev_handle, off_t *bufpos, size_t *buflen, off_t *pos, void *dst,
     130    size_t size, size_t block_size)
    64131{
    65132        off_t offset = 0;
     
    79146                         * destination buffer.
    80147                         */
    81                         memcpy(dst + offset, buffer + *bufpos, rd);
     148                        memcpy(dst + offset, dev_buffer + *bufpos, rd);
    82149                        offset += rd;
    83150                        *bufpos += rd;
     
    89156                        /* Refill the communication buffer with a new block. */
    90157                        ipcarg_t retval;
    91                         int rc = async_req_2_1(phone, RD_READ_BLOCK,
     158                        int rc = async_req_2_1(dev_phone, RD_READ_BLOCK,
    92159                            *pos / block_size, block_size, &retval);
    93160                        if ((rc != EOK) || (retval != EOK))
     
    101168        return true;
    102169}
    103 
    104 int dev_phone = -1;             /* FIXME */
    105 void *dev_buffer = NULL;        /* FIXME */
    106170
    107171block_t *block_get(dev_handle_t dev_handle, off_t offset, size_t bs)
     
    127191        b->size = bs;
    128192
    129         if (!blockread(dev_phone, dev_buffer, &bufpos, &buflen, &pos, b->data,
     193        if (!block_read(dev_handle, &bufpos, &buflen, &pos, b->data,
    130194            bs, bs)) {
    131195                free(b->data);
Note: See TracChangeset for help on using the changeset viewer.