Subversion Repositories HelenOS

Rev

Rev 2396 | Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * Copyright (c) 2007 Konopa-Jelen-Majer
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  *
  9.  * - Redistributions of source code must retain the above copyright
  10.  *   notice, this list of conditions and the following disclaimer.
  11.  * - Redistributions in binary form must reproduce the above copyright
  12.  *   notice, this list of conditions and the following disclaimer in the
  13.  *   documentation and/or other materials provided with the distribution.
  14.  * - The name of the author may not be used to endorse or promote products
  15.  *   derived from this software without specific prior written permission.
  16.  *
  17.  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  18.  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  19.  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  20.  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  21.  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  22.  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  23.  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  24.  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26.  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27.  */
  28.  
  29. /** @addtogroup RamDiskTask
  30.  * @{
  31.  */
  32.  
  33. /**
  34.  * @file    rd.c
  35.  * @brief   Initial RAM disk for HelenOS.
  36.  */
  37.  
  38. #include <ipc/ipc.h>
  39. #include <ipc/services.h>
  40. #include <ipc/ns.h>
  41. #include <sysinfo.h>
  42. #include <as.h>
  43. #include <ddi.h>
  44. #include <align.h>
  45. #include <bool.h>
  46. #include <errno.h>
  47. #include <async.h>
  48. #include <stdlib.h>
  49. #include <unistd.h>
  50. #include <align.h>
  51. #include <async.h>
  52. #include <ddi.h>
  53. #include <libarch/ddi.h>
  54. #include <stdio.h>
  55. #include "rd.h"
  56. #include "../console/console.h"
  57. #include "../share/base_const.h"
  58.  
  59.  
  60. #define CON_CONN_ATTEMPTS   1000
  61.  
  62. static int con_phone;
  63. static void *rd_addr;
  64. static void *fs_addr;
  65.  
  66. static int connect_to_con(int *phone, int attempts);
  67.  
  68. static void rd_connection(ipc_callid_t iid, ipc_call_t *icall)
  69. {
  70.     ipc_callid_t callid;
  71.     ipc_call_t call;
  72.     int retval;
  73.  
  74.     ipc_answer_fast(iid, 0, 0, 0);
  75.     ipcarg_t dst, offset;
  76.  
  77.     while (1) {
  78.         callid = async_get_call(&call);
  79.         switch (IPC_GET_METHOD(call)) {
  80.             case IPC_M_PHONE_HUNGUP:
  81.                 ipc_answer_fast(callid, 0,0,0);
  82.                 return;
  83.             case IPC_M_AS_AREA_SEND:
  84.                 //print_console("Receiving shared area from FS...");
  85.                 ipc_answer_fast(callid, 0, (uintptr_t)fs_addr, 0);
  86.                 //print_console("OK\n");
  87.                 continue;
  88.             case RD_READ_BLOCK:        
  89.                 offset = IPC_GET_ARG1(call);
  90.                 memcpy((void *)fs_addr, rd_addr+offset, BLOCK_SIZE);
  91.                 retval = OK;
  92.                 break;
  93.             default: retval = EINVAL;
  94.         }
  95.         ipc_answer_fast(callid, retval, 0, 0);
  96.     }  
  97. }
  98.  
  99.  
  100. static bool rd_init(void)
  101. {
  102.    
  103.     int retval, flags;
  104.  
  105.     size_t rd_size = sysinfo_value("rd.size");
  106.     void * rd_ph_addr = (void *) sysinfo_value("rd.address.physical");
  107.     int rd_color = (int) sysinfo_value("rd.address.color");
  108.    
  109.     if (rd_size == 0)
  110.         return false;
  111.    
  112.     rd_addr = as_get_mappable_page(rd_size, rd_color);
  113.    
  114.     //print_console("Physmem mapping...");
  115.     flags = AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE;
  116.     retval = physmem_map(rd_ph_addr, rd_addr, ALIGN_UP(rd_size, PAGE_SIZE) >> PAGE_WIDTH, flags);
  117.  
  118.     if (retval < 0) {
  119.         //print_console_int("%d\n", retval);
  120.         return FALSE;
  121.     }  
  122.     //print_console("OK\n");
  123.  
  124.     size_t fs_size = ALIGN_UP(BLOCK_SIZE * sizeof(char), PAGE_SIZE);
  125.     fs_addr = as_get_mappable_page(fs_size, PAGE_COLOR((uintptr_t)fs_addr));
  126.  
  127.     return TRUE;
  128. }
  129.  
  130. int connect_to_con(int *phone, int attempts)
  131. {
  132.    
  133.     int i;
  134.  
  135.     if (attempts <= 0)
  136.         return FALSE;
  137.            
  138.     for (i = 1; i <= attempts; i++) {
  139.         *phone = ipc_connect_me_to(PHONE_NS, SERVICE_CONSOLE, 0);
  140.        
  141.         if (*phone > 0)
  142.             return TRUE;
  143.        
  144.         usleep(10000);
  145.     }
  146.    
  147.     if (*phone >= 0)
  148.         return TRUE;
  149.  
  150.     return FALSE;
  151. }
  152.  
  153. int main(int argc, char **argv)
  154. {
  155.    
  156.     /* Connection to SERVICE_CONSOLE service. */
  157.     if (!connect_to_con(&con_phone, CON_CONN_ATTEMPTS)) {
  158.         return -1;
  159.     }
  160.    
  161.     //print_console("RAMDISK task\n");
  162.  
  163.     if (rd_init()) {
  164.         ipcarg_t phonead;
  165.        
  166.         async_set_client_connection(rd_connection);
  167.        
  168.         /* Register service at nameserver */
  169.         if (ipc_connect_to_me(PHONE_NS, SERVICE_RD, 0, &phonead) != 0)
  170.             return -1;
  171.        
  172.         async_manager();
  173.        
  174.         /* Never reached */
  175.         return 0;
  176.     }
  177.    
  178.     return -1;
  179. }
  180.  
  181. /**
  182.  * @}
  183.  */
  184.