51,12 → 51,8 |
#include <align.h> |
#include <async.h> |
#include <futex.h> |
#include <stdio.h> |
#include <ipc/devmap.h> |
#include "rd.h" |
|
#define NAME "rd" |
|
/** Pointer to the ramdisk's image. */ |
static void *rd_addr; |
/** Size of the ramdisk. */ |
81,24 → 77,45 |
ipc_call_t call; |
int retval; |
void *fs_va = NULL; |
off_t offset; |
size_t block_size; |
size_t maxblock_size; |
ipcarg_t offset; |
|
/* |
* We allocate VA for communication per connection. |
* This allows us to potentionally have more clients and work |
* concurrently. |
*/ |
fs_va = as_get_mappable_page(ALIGN_UP(BLOCK_SIZE, PAGE_SIZE)); |
if (!fs_va) { |
/* |
* Hang up the phone if we cannot proceed any further. |
* This is the answer to the call that opened the connection. |
*/ |
ipc_answer_0(iid, EHANGUP); |
return; |
} else { |
/* |
* Answer the first IPC_M_CONNECT_ME_TO call. |
* Return supported block size as ARG1. |
*/ |
ipc_answer_0(iid, EOK); |
ipc_answer_1(iid, EOK, BLOCK_SIZE); |
} |
|
/* |
* Now we wait for the client to send us its communication as_area. |
*/ |
int flags; |
if (ipc_share_out_receive(&callid, &maxblock_size, &flags)) { |
fs_va = as_get_mappable_page(maxblock_size); |
if (fs_va) { |
size_t size; |
if (ipc_share_out_receive(&callid, &size, NULL)) { |
if (size >= BLOCK_SIZE) { |
/* |
* The client sends an as_area that can absorb the whole |
* block. |
*/ |
(void) ipc_share_out_finalize(callid, fs_va); |
} else { |
/* |
* The client offered as_area too small. |
* Close the connection. |
*/ |
ipc_answer_0(callid, EHANGUP); |
return; |
} |
124,16 → 141,8 |
return; |
case RD_READ_BLOCK: |
offset = IPC_GET_ARG1(call); |
block_size = IPC_GET_ARG2(call); |
if (block_size > maxblock_size) { |
if (offset * BLOCK_SIZE > rd_size - BLOCK_SIZE) { |
/* |
* Maximum block size exceeded. |
*/ |
retval = ELIMIT; |
break; |
} |
if (offset * block_size > rd_size - block_size) { |
/* |
* Reading past the end of the device. |
*/ |
retval = ELIMIT; |
140,22 → 149,14 |
break; |
} |
futex_down(&rd_futex); |
memcpy(fs_va, rd_addr + offset * block_size, block_size); |
memcpy(fs_va, rd_addr + offset, BLOCK_SIZE); |
futex_up(&rd_futex); |
retval = EOK; |
break; |
case RD_WRITE_BLOCK: |
offset = IPC_GET_ARG1(call); |
block_size = IPC_GET_ARG2(call); |
if (block_size > maxblock_size) { |
if (offset * BLOCK_SIZE > rd_size - BLOCK_SIZE) { |
/* |
* Maximum block size exceeded. |
*/ |
retval = ELIMIT; |
break; |
} |
if (offset * block_size > rd_size - block_size) { |
/* |
* Writing past the end of the device. |
*/ |
retval = ELIMIT; |
162,7 → 163,7 |
break; |
} |
futex_up(&rd_futex); |
memcpy(rd_addr + offset * block_size, fs_va, block_size); |
memcpy(rd_addr + offset, fs_va, BLOCK_SIZE); |
futex_down(&rd_futex); |
retval = EOK; |
break; |
180,113 → 181,39 |
} |
} |
|
static int driver_register(char *name) |
{ |
ipcarg_t retval; |
aid_t req; |
ipc_call_t answer; |
int phone; |
ipcarg_t callback_phonehash; |
|
phone = ipc_connect_me_to_blocking(PHONE_NS, SERVICE_DEVMAP, DEVMAP_DRIVER, 0); |
if (phone < 0) { |
printf(NAME ": Failed to connect to device mapper\n"); |
return -1; |
} |
|
req = async_send_2(phone, DEVMAP_DRIVER_REGISTER, 0, 0, &answer); |
|
retval = ipc_data_write_start(phone, (char *) name, strlen(name) + 1); |
|
if (retval != EOK) { |
async_wait_for(req, NULL); |
return -1; |
} |
|
async_set_client_connection(rd_connection); |
|
ipc_connect_to_me(phone, 0, 0, 0, &callback_phonehash); |
async_wait_for(req, &retval); |
|
return phone; |
} |
|
static int device_register(int driver_phone, char *name, int *handle) |
{ |
ipcarg_t retval; |
aid_t req; |
ipc_call_t answer; |
|
req = async_send_2(driver_phone, DEVMAP_DEVICE_REGISTER, 0, 0, &answer); |
|
retval = ipc_data_write_start(driver_phone, (char *) name, strlen(name) + 1); |
|
if (retval != EOK) { |
async_wait_for(req, NULL); |
return retval; |
} |
|
async_wait_for(req, &retval); |
|
if (handle != NULL) |
*handle = -1; |
|
if (EOK == retval) { |
if (NULL != handle) |
*handle = (int) IPC_GET_ARG1(answer); |
} |
|
return retval; |
} |
|
/** Prepare the ramdisk image for operation. */ |
static bool rd_init(void) |
{ |
int retval, flags; |
|
rd_size = sysinfo_value("rd.size"); |
void *rd_ph_addr = (void *) sysinfo_value("rd.address.physical"); |
|
if (rd_size == 0) { |
printf(NAME ": No RAM disk found\n"); |
if (rd_size == 0) |
return false; |
} |
|
rd_addr = as_get_mappable_page(rd_size); |
|
int flags = AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE; |
int retval = physmem_map(rd_ph_addr, rd_addr, |
flags = AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE; |
retval = physmem_map(rd_ph_addr, rd_addr, |
ALIGN_UP(rd_size, PAGE_SIZE) >> PAGE_WIDTH, flags); |
|
if (retval < 0) { |
printf(NAME ": Error mapping RAM disk\n"); |
if (retval < 0) |
return false; |
} |
|
printf(NAME ": Found RAM disk at %p, %d bytes\n", rd_ph_addr, rd_size); |
|
int driver_phone = driver_register(NAME); |
if (driver_phone < 0) { |
printf(NAME ": Unable to register driver\n"); |
return false; |
} |
|
int dev_handle; |
if (EOK != device_register(driver_phone, "initrd", &dev_handle)) { |
ipc_hangup(driver_phone); |
printf(NAME ": Unable to register device\n"); |
return false; |
} |
|
return true; |
} |
|
int main(int argc, char **argv) |
{ |
printf(NAME ": HelenOS RAM disk server\n"); |
if (rd_init()) { |
ipcarg_t phonead; |
|
if (!rd_init()) |
async_set_client_connection(rd_connection); |
|
/* Register service at nameserver */ |
if (ipc_connect_to_me(PHONE_NS, SERVICE_RD, 0, 0, &phonead) != 0) |
return -1; |
|
printf(NAME ": Accepting connections\n"); |
async_manager(); |
|
/* Never reached */ |
293,6 → 220,9 |
return 0; |
} |
|
return -1; |
} |
|
/** |
* @} |
*/ |