Changeset 3fb0fec in mainline


Ignore:
Timestamp:
2011-07-26T18:29:44Z (13 years ago)
Author:
Jakub Jermar <jakub@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
7eb0fed8
Parents:
efcebe1 (diff), aa865ee (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge mainline changes.

Files:
16 added
3 deleted
42 edited
2 moved

Legend:

Unmodified
Added
Removed
  • HelenOS.config

    refcebe1 r3fb0fec  
    8888@ "tmpfs" TMPFS image
    8989@ "fat" FAT16 image
     90@ "ext2fs" EXT2 image
    9091! RDFMT (choice)
    9192
  • boot/Makefile

    refcebe1 r3fb0fec  
    5050        $(MKFAT) 1048576 $(DIST_PATH) $@
    5151endif
     52ifeq ($(RDFMT),ext2fs)
     53        $(MKEXT2) 1048576 $(DIST_PATH) $@
     54endif
    5255
    5356build_dist: clean_dist
  • boot/Makefile.common

    refcebe1 r3fb0fec  
    5656MKTMPFS = $(TOOLS_PATH)/mktmpfs.py
    5757MKFAT = $(TOOLS_PATH)/mkfat.py
     58MKEXT2 = $(TOOLS_PATH)/mkext2.py
    5859MKUIMAGE = $(TOOLS_PATH)/mkuimage.py
    5960
     
    8283ifeq ($(RDFMT),fat)
    8384        INIT_TASKS += $(USPACE_PATH)/srv/fs/fat/fat
     85endif
     86
     87ifeq ($(RDFMT),ext2fs)
     88        INIT_TASKS += $(USPACE_PATH)/srv/fs/ext2fs/ext2fs
    8489endif
    8590
  • kernel/generic/include/lib/elf.h

    refcebe1 r3fb0fec  
    4242 * current ELF version
    4343 */
    44 #define EV_CURRENT  1
     44#define EV_CURRENT      1
    4545
    4646/**
    4747 * ELF types
    4848 */
    49 #define ET_NONE    0       /* No type */
    50 #define ET_REL     1       /* Relocatable file */
    51 #define ET_EXEC    2       /* Executable */
    52 #define ET_DYN     3       /* Shared object */
    53 #define ET_CORE    4       /* Core */
    54 #define ET_LOPROC  0xff00  /* Processor specific */
    55 #define ET_HIPROC  0xffff  /* Processor specific */
     49#define ET_NONE         0       /* No type */
     50#define ET_REL          1       /* Relocatable file */
     51#define ET_EXEC         2       /* Executable */
     52#define ET_DYN          3       /* Shared object */
     53#define ET_CORE         4       /* Core */
     54#define ET_LOPROC       0xff00  /* Processor specific */
     55#define ET_HIPROC       0xffff  /* Processor specific */
    5656
    5757/**
    5858 * ELF machine types
    5959 */
    60 #define EM_NO           0   /* No machine */
    61 #define EM_SPARC        2   /* SPARC */
    62 #define EM_386          3   /* i386 */
    63 #define EM_MIPS         8   /* MIPS RS3000 */
    64 #define EM_MIPS_RS3_LE  10  /* MIPS RS3000 LE */
    65 #define EM_PPC          20  /* PPC32 */
    66 #define EM_PPC64        21  /* PPC64 */
    67 #define EM_ARM          40  /* ARM */
    68 #define EM_SPARCV9      43  /* SPARC64 */
    69 #define EM_IA_64        50  /* IA-64 */
    70 #define EM_X86_64       62  /* AMD64/EMT64 */
     60#define EM_NO           0       /* No machine */
     61#define EM_SPARC        2       /* SPARC */
     62#define EM_386          3       /* i386 */
     63#define EM_MIPS         8       /* MIPS RS3000 */
     64#define EM_MIPS_RS3_LE  10      /* MIPS RS3000 LE */
     65#define EM_PPC          20      /* PPC32 */
     66#define EM_PPC64        21      /* PPC64 */
     67#define EM_ARM          40      /* ARM */
     68#define EM_SPARCV9      43      /* SPARC64 */
     69#define EM_IA_64        50      /* IA-64 */
     70#define EM_X86_64       62      /* AMD64/EMT64 */
    7171
    7272/**
    7373 * ELF identification indexes
    7474 */
    75 #define EI_MAG0        0
    76 #define EI_MAG1        1
    77 #define EI_MAG2        2
    78 #define EI_MAG3        3
    79 #define EI_CLASS       4   /* File class */
    80 #define EI_DATA        5   /* Data encoding */
    81 #define EI_VERSION     6   /* File version */
    82 #define EI_OSABI       7
    83 #define EI_ABIVERSION  8
    84 #define EI_PAD         9   /* Start of padding bytes */
    85 #define EI_NIDENT      16  /* ELF identification table size */
     75#define EI_MAG0         0
     76#define EI_MAG1         1
     77#define EI_MAG2         2
     78#define EI_MAG3         3
     79#define EI_CLASS        4               /* File class */
     80#define EI_DATA         5               /* Data encoding */
     81#define EI_VERSION      6               /* File version */
     82#define EI_OSABI        7
     83#define EI_ABIVERSION   8
     84#define EI_PAD          9               /* Start of padding bytes */
     85#define EI_NIDENT       16              /* ELF identification table size */
    8686
    8787/**
    8888 * ELF magic number
    8989 */
    90 #define ELFMAG0  0x7f
    91 #define ELFMAG1  'E'
    92 #define ELFMAG2  'L'
    93 #define ELFMAG3  'F'
     90#define ELFMAG0         0x7f
     91#define ELFMAG1         'E'
     92#define ELFMAG2         'L'
     93#define ELFMAG3         'F'
    9494
    9595/**
    9696 * ELF file classes
    9797 */
    98 #define ELFCLASSNONE  0
    99 #define ELFCLASS32    1
    100 #define ELFCLASS64    2
     98#define ELFCLASSNONE    0
     99#define ELFCLASS32      1
     100#define ELFCLASS64      2
    101101
    102102/**
    103103 * ELF data encoding types
    104104 */
    105 #define ELFDATANONE  0
    106 #define ELFDATA2LSB  1  /* Least significant byte first (little endian) */
    107 #define ELFDATA2MSB  2  /* Most signigicant byte first (big endian) */
    108 
    109 /**
    110  * ELF error return codes
    111  */
    112 #define EE_OK             0  /* No error */
    113 #define EE_INVALID        1  /* Invalid ELF image */
    114 #define EE_MEMORY         2  /* Cannot allocate address space */
    115 #define EE_INCOMPATIBLE   3  /* ELF image is not compatible with current architecture */
    116 #define EE_UNSUPPORTED    4  /* Non-supported ELF (e.g. dynamic ELFs) */
    117 #define EE_LOADER         5  /* The image is actually a program loader */
    118 #define EE_IRRECOVERABLE  6
     105#define ELFDATANONE     0
     106#define ELFDATA2LSB     1               /* Least significant byte first (little endian) */
     107#define ELFDATA2MSB     2               /* Most signigicant byte first (big endian) */
    119108
    120109/**
    121110 * ELF section types
    122111 */
    123 #define SHT_NULL      0
    124 #define SHT_PROGBITS  1
    125 #define SHT_SYMTAB    2
    126 #define SHT_STRTAB    3
    127 #define SHT_RELA      4
    128 #define SHT_HASH      5
    129 #define SHT_DYNAMIC   6
    130 #define SHT_NOTE      7
    131 #define SHT_NOBITS    8
    132 #define SHT_REL       9
    133 #define SHT_SHLIB     10
    134 #define SHT_DYNSYM    11
    135 #define SHT_LOOS      0x60000000
    136 #define SHT_HIOS      0x6fffffff
    137 #define SHT_LOPROC    0x70000000
    138 #define SHT_HIPROC    0x7fffffff
    139 #define SHT_LOUSER    0x80000000
    140 #define SHT_HIUSER    0xffffffff
     112#define SHT_NULL                0
     113#define SHT_PROGBITS            1
     114#define SHT_SYMTAB              2
     115#define SHT_STRTAB              3
     116#define SHT_RELA                4
     117#define SHT_HASH                5
     118#define SHT_DYNAMIC             6
     119#define SHT_NOTE                7
     120#define SHT_NOBITS              8
     121#define SHT_REL                 9
     122#define SHT_SHLIB               10
     123#define SHT_DYNSYM              11
     124#define SHT_LOOS                0x60000000
     125#define SHT_HIOS                0x6fffffff
     126#define SHT_LOPROC              0x70000000
     127#define SHT_HIPROC              0x7fffffff
     128#define SHT_LOUSER              0x80000000
     129#define SHT_HIUSER              0xffffffff
    141130
    142131/**
    143132 * ELF section flags
    144133 */
    145 #define SHF_WRITE      0x1
    146 #define SHF_ALLOC      0x2
    147 #define SHF_EXECINSTR  0x4
    148 #define SHF_TLS        0x400
    149 #define SHF_MASKPROC   0xf0000000
     134#define SHF_WRITE               0x1
     135#define SHF_ALLOC               0x2
     136#define SHF_EXECINSTR           0x4
     137#define SHF_TLS                 0x400
     138#define SHF_MASKPROC            0xf0000000
     139
     140/** Macros for decomposing elf_symbol.st_info into binging and type */
     141#define ELF_ST_BIND(i)          ((i) >> 4)
     142#define ELF_ST_TYPE(i)          ((i) & 0x0f)
     143#define ELF_ST_INFO(b, t)       (((b) << 4) + ((t) & 0x0f))
    150144
    151145/**
    152146 * Symbol binding
    153147 */
    154 #define STB_LOCAL   0
    155 #define STB_GLOBAL  1
    156 #define STB_WEAK    2
    157 #define STB_LOPROC  13
    158 #define STB_HIPROC  15
     148#define STB_LOCAL               0
     149#define STB_GLOBAL              1
     150#define STB_WEAK                2
     151#define STB_LOPROC              13
     152#define STB_HIPROC              15
    159153
    160154/**
    161155 * Symbol types
    162156 */
    163 #define STT_NOTYPE   0
    164 #define STT_OBJECT   1
    165 #define STT_FUNC     2
    166 #define STT_SECTION  3
    167 #define STT_FILE     4
    168 #define STT_LOPROC   13
    169 #define STT_HIPROC   15
     157#define STT_NOTYPE              0
     158#define STT_OBJECT              1
     159#define STT_FUNC                2
     160#define STT_SECTION             3
     161#define STT_FILE                4
     162#define STT_LOPROC              13
     163#define STT_HIPROC              15
    170164
    171165/**
    172166 * Program segment types
    173167 */
    174 #define PT_NULL     0
    175 #define PT_LOAD     1
    176 #define PT_DYNAMIC  2
    177 #define PT_INTERP   3
    178 #define PT_NOTE     4
    179 #define PT_SHLIB    5
    180 #define PT_PHDR     6
    181 #define PT_LOPROC   0x70000000
    182 #define PT_HIPROC   0x7fffffff
     168#define PT_NULL                 0
     169#define PT_LOAD                 1
     170#define PT_DYNAMIC              2
     171#define PT_INTERP               3
     172#define PT_NOTE                 4
     173#define PT_SHLIB                5
     174#define PT_PHDR                 6
     175#define PT_LOPROC               0x70000000
     176#define PT_HIPROC               0x7fffffff
    183177
    184178/**
    185179 * Program segment attributes.
    186180 */
    187 #define PF_X  1
    188 #define PF_W  2
    189 #define PF_R  4
     181#define PF_X    1
     182#define PF_W    2
     183#define PF_R    4
    190184
    191185/**
     
    195189 * ELF object file specifications. They are the only types used
    196190 * in ELF header.
    197  *
    198191 */
    199192typedef uint64_t elf_xword;
     
    207200 *
    208201 * These types are specific for 32-bit format.
    209  *
    210202 */
    211203typedef uint32_t elf32_addr;
     
    216208 *
    217209 * These types are specific for 64-bit format.
    218  *
    219210 */
    220211typedef uint64_t elf64_addr;
     
    238229        elf_half e_shstrndx;
    239230};
    240 
    241231struct elf64_header {
    242232        uint8_t e_ident[EI_NIDENT];
     
    256246};
    257247
    258 /**
     248/*
    259249 * ELF segment header.
    260250 * Segments headers are also known as program headers.
     
    270260        elf_word p_align;
    271261};
    272 
    273262struct elf64_segment_header {
    274263        elf_word p_type;
     
    282271};
    283272
    284 /**
     273/*
    285274 * ELF section header
    286275 */
     
    297286        elf_word sh_entsize;
    298287};
    299 
    300288struct elf64_section_header {
    301289        elf_word sh_name;
     
    311299};
    312300
    313 /**
     301/*
    314302 * ELF symbol table entry
    315303 */
     
    322310        elf_half st_shndx;
    323311};
    324 
    325312struct elf64_symbol {
    326313        elf_word st_name;
     
    332319};
    333320
     321/*
     322 * ELF note segment entry
     323 */
     324struct elf32_note {
     325        elf_word namesz;
     326        elf_word descsz;
     327        elf_word type;
     328};
     329struct elf64_note {
     330        elf_xword namesz;
     331        elf_xword descsz;
     332        elf_xword type;
     333};
     334
    334335#ifdef __32_BITS__
    335336typedef struct elf32_header elf_header_t;
     
    337338typedef struct elf32_section_header elf_section_header_t;
    338339typedef struct elf32_symbol elf_symbol_t;
     340typedef struct elf32_note elf_note_t;
    339341#endif
    340 
    341342#ifdef __64_BITS__
    342343typedef struct elf64_header elf_header_t;
     
    344345typedef struct elf64_section_header elf_section_header_t;
    345346typedef struct elf64_symbol elf_symbol_t;
     347typedef struct elf64_note elf_note_t;
    346348#endif
    347 
    348 extern const char *elf_error(unsigned int rc);
    349349
    350350/** Interpreter string used to recognize the program loader */
  • kernel/generic/include/mm/as.h

    refcebe1 r3fb0fec  
    307307extern mem_backend_t phys_backend;
    308308
    309 /**
    310  * This flags is passed when running the loader, otherwise elf_load()
    311  * would return with a EE_LOADER error code.
    312  *
    313  */
    314 #define ELD_F_NONE    0
    315 #define ELD_F_LOADER  1
    316 
    317 extern unsigned int elf_load(elf_header_t *, as_t *, unsigned int);
    318 
    319309/* Address space area related syscalls. */
    320310extern sysarg_t sys_as_area_create(uintptr_t, size_t, unsigned int);
  • kernel/generic/src/lib/elf.c

    refcebe1 r3fb0fec  
    4747#include <macros.h>
    4848#include <arch.h>
     49
     50#include <lib/elf_load.h>
    4951
    5052static const char *error_codes[] = {
  • kernel/generic/src/proc/program.c

    refcebe1 r3fb0fec  
    4848#include <ipc/ipcrsc.h>
    4949#include <security/cap.h>
    50 #include <lib/elf.h>
     50#include <lib/elf_load.h>
    5151#include <errno.h>
    5252#include <print.h>
  • tools/mkfat.py

    refcebe1 r3fb0fec  
    3737import xstruct
    3838import array
    39 
    40 exclude_names = set(['.svn', '.bzr'])
    41 
    42 def align_up(size, alignment):
    43         "Return size aligned up to alignment"
    44        
    45         if (size % alignment == 0):
    46                 return size
    47        
    48         return ((size // alignment) + 1) * alignment
     39from imgutil import *
    4940
    5041def subtree_size(root, cluster_size, dirent_size):
     
    5445        files = 2
    5546       
    56         for name in os.listdir(root):
    57                 canon = os.path.join(root, name)
    58                
    59                 if (os.path.isfile(canon) and (not name in exclude_names)):
    60                         size += align_up(os.path.getsize(canon), cluster_size)
     47        for item in listdir_items(root):
     48                if item.is_file:
     49                        size += align_up(item.size, cluster_size)
    6150                        files += 1
    62                
    63                 if (os.path.isdir(canon) and (not name in exclude_names)):
    64                         size += subtree_size(canon, cluster_size, dirent_size)
     51                elif item.is_dir:
     52                        size += subtree_size(item.path, cluster_size, dirent_size)
    6553                        files += 1
    6654       
     
    7260        return len(os.listdir(root))
    7361
    74 def write_file(path, outf, cluster_size, data_start, fat, reserved_clusters):
     62def write_file(item, outf, cluster_size, data_start, fat, reserved_clusters):
    7563        "Store the contents of a file"
    7664       
    77         size = os.path.getsize(path)
    7865        prev = -1
    7966        first = 0
    8067       
    81         inf = open(path, "rb")
    82         rd = 0;
    83         while (rd < size):
     68        for data in chunks(item, cluster_size):
    8469                empty_cluster = fat.index(0)
    8570                fat[empty_cluster] = 0xffff
     
    9277                prev = empty_cluster
    9378               
    94                 data = bytes(inf.read(cluster_size));
    9579                outf.seek(data_start + (empty_cluster - reserved_clusters) * cluster_size)
    9680                outf.write(data)
    97                 rd += len(data)
    98         inf.close()
    99        
    100         return first, size
     81       
     82        return first, item.size
    10183
    10284def write_directory(directory, outf, cluster_size, data_start, fat, reserved_clusters, dirent_size, empty_cluster):
     
    303285                empty_cluster = 0
    304286       
    305         for name in os.listdir(root):
    306                 canon = os.path.join(root, name)
    307                
    308                 if (os.path.isfile(canon) and (not name in exclude_names)):
    309                         rv = write_file(canon, outf, cluster_size, data_start, fat, reserved_clusters)
    310                         directory.append(create_dirent(name, False, rv[0], rv[1]))
    311                
    312                 if (os.path.isdir(canon) and (not name in exclude_names)):
    313                         rv = recursion(False, canon, outf, cluster_size, root_start, data_start, fat, reserved_clusters, dirent_size, empty_cluster)
    314                         directory.append(create_dirent(name, True, rv[0], rv[1]))
     287        for item in listdir_items(root):               
     288                if item.is_file:
     289                        rv = write_file(item, outf, cluster_size, data_start, fat, reserved_clusters)
     290                        directory.append(create_dirent(item.name, False, rv[0], rv[1]))
     291                elif item.is_dir:
     292                        rv = recursion(False, item.path, outf, cluster_size, root_start, data_start, fat, reserved_clusters, dirent_size, empty_cluster)
     293                        directory.append(create_dirent(item.name, True, rv[0], rv[1]))
    315294       
    316295        if (head):
  • tools/mktmpfs.py

    refcebe1 r3fb0fec  
    3535import os
    3636import xstruct
    37 
    38 exclude_names = set(['.svn', '.bzr'])
     37from imgutil import listdir_items, chunks
    3938
    4039HEADER = """little:
     
    7170        "Recursive directory walk"
    7271       
    73         for name in os.listdir(root):
    74                 canon = os.path.join(root, name)
    75                
    76                 if (os.path.isfile(canon) and (not name in exclude_names)):
    77                         size = os.path.getsize(canon)
    78                        
    79                         dentry = xstruct.create(DENTRY_FILE % len(name))
     72        for item in listdir_items(root):               
     73                if item.is_file:                       
     74                        dentry = xstruct.create(DENTRY_FILE % len(item.name))
    8075                        dentry.kind = TMPFS_FILE
    81                         dentry.fname_len = len(name)
    82                         dentry.fname = name.encode('ascii')
    83                         dentry.flen = size
     76                        dentry.fname_len = len(item.name)
     77                        dentry.fname = item.name.encode('ascii')
     78                        dentry.flen = item.size
    8479                       
    8580                        outf.write(dentry.pack())
    8681                       
    87                         inf = open(canon, "rb")
    88                         rd = 0;
    89                         while (rd < size):
    90                                 data = inf.read(4096);
     82                        for data in chunks(item, 4096):
    9183                                outf.write(data)
    92                                 rd += len(data)
    93                         inf.close()
    9484               
    95                 if (os.path.isdir(canon) and (not name in exclude_names)):
    96                         dentry = xstruct.create(DENTRY_DIRECTORY % len(name))
     85                elif item.is_dir:
     86                        dentry = xstruct.create(DENTRY_DIRECTORY % len(item.name))
    9787                        dentry.kind = TMPFS_DIRECTORY
    98                         dentry.fname_len = len(name)
    99                         dentry.fname = name.encode('ascii')
     88                        dentry.fname_len = len(item.name)
     89                        dentry.fname = item.name.encode('ascii')
    10090                       
    10191                        outf.write(dentry.pack())
    10292                       
    103                         recursion(canon, outf)
     93                        recursion(item.path, outf)
    10494                       
    10595                        dentry = xstruct.create(DENTRY_NONE)
  • tools/xstruct.py

    refcebe1 r3fb0fec  
    11#
    22# Copyright (c) 2008 Martin Decky
     3# Copyright (c) 2011 Martin Sucha
    34# All rights reserved.
    45#
     
    3132
    3233import struct
     34import types
     35
     36ranges = {
     37        'B': ((int, long), 0x00, 0xff),
     38        'H': ((int, long), 0x0000, 0xffff),
     39        'L': ((int, long), 0x00000000, 0xffffffff),
     40        'Q': ((int, long), 0x0000000000000000, 0xffffffffffffffff),
     41        'b': ((int, long), -0x80, 0x7f),
     42        'h': ((int, long), -0x8000, 0x7fff),
     43        'l': ((int, long), -0x80000000, 0x7fffffff) ,
     44        'q': ((int, long), -0x8000000000000000, 0x7fffffffffffffff),
     45}
     46
     47def check_range(varname, fmt, value):
     48        if value == None:
     49                raise ValueError('Variable "%s" not set' % varname)
     50        if not fmt in ranges:
     51                return
     52        vartype, varmin, varmax = ranges[fmt]
     53        if not isinstance(value, vartype):
     54                raise ValueError('Variable "%s" is %s but should be %s' %
     55                                 (varname, str(type(value)), str(vartype)))
     56        if value < varmin or value > varmax:
     57                raise ValueError('Variable "%s" value %s out of range %s..%s' %
     58                                 (varname, repr(value), repr(varmin), repr(varmax)))
    3359
    3460class Struct:
     
    3864        def pack(self):
    3965                args = []
    40                 for variable in self._args_:
    41                         if (isinstance(self.__dict__[variable], list)):
    42                                 for item in self.__dict__[variable]:
     66                for variable, fmt, length in self._args_:
     67                        value = self.__dict__[variable]
     68                        if isinstance(value, list):
     69                                if length != None and length != len(value):
     70                                        raise ValueError('Variable "%s" length %u does not match %u' %
     71                                                      (variable, len(value), length))
     72                                for index, item in enumerate(value):
     73                                        check_range(variable + '[' + repr(index) + ']', fmt, item)
    4374                                        args.append(item)
    4475                        else:
    45                                 args.append(self.__dict__[variable])
    46                
     76                                check_range(variable, fmt, value)
     77                                args.append(value)             
    4778                return struct.pack(self._format_, *args)
     79       
     80        def unpack(self, data):
     81                values = struct.unpack(self._format_, data)
     82                i = 0
     83                for variable, fmt, length in self._args_:
     84                        self.__dict__[variable] = values[i]
     85                        i += 1
    4886
    4987def create(definition):
     
    77115                        subtokens = token.split("[")
    78116                       
     117                        length = None
    79118                        if (len(subtokens) > 1):
    80                                 format += "%d" % int(subtokens[1].split("]")[0])
     119                                length = int(subtokens[1].split("]")[0])
     120                                format += "%d" % length
    81121                       
    82122                        format += variable
    83123                       
    84124                        inst.__dict__[subtokens[0]] = None
    85                         args.append(subtokens[0])
     125                        args.append((subtokens[0], variable, length))
    86126                       
    87127                        variable = None
  • uspace/app/bdsh/cmds/modules/cp/cp.c

    refcebe1 r3fb0fec  
    7171        size_t blen, int vb)
    7272{
    73         int fd1, fd2, bytes = 0;
    74         off64_t total = 0;
     73        int fd1, fd2, bytes;
     74        off64_t total;
    7575        int64_t copied = 0;
    7676        char *buff = NULL;
     
    104104        }
    105105
    106         for (;;) {
    107                 ssize_t res;
    108                 size_t written = 0;
    109 
    110                 bytes = read(fd1, buff, blen);
    111                 if (bytes <= 0)
     106        while ((bytes = read_all(fd1, buff, blen)) > 0) {
     107                if ((bytes = write_all(fd2, buff, bytes)) < 0)
    112108                        break;
    113109                copied += bytes;
    114                 res = bytes;
    115                 do {
    116                         /*
    117                          * Theoretically, it may not be enough to call write()
    118                          * only once. Also the previous read() may have
    119                          * returned less data than requested.
    120                          */
    121                         bytes = write(fd2, buff + written, res);
    122                         if (bytes < 0)
    123                                 goto err;
    124                         written += bytes;
    125                         res -= bytes;
    126                 } while (res > 0);
    127 
    128                 /* TODO: re-insert assert() once this is stand alone,
    129                  * removed as abort() exits the entire shell
    130                  */
    131                 if (res != 0) {
    132                         printf("\n%zd more bytes than actually exist were copied\n", res);
    133                         goto err;
    134                 }
    135110        }
    136111
    137112        if (bytes < 0) {
    138 err:
    139113                printf("\nError copying %s, (%d)\n", src, bytes);
    140114                copied = bytes;
  • uspace/app/bdsh/compl.c

    refcebe1 r3fb0fec  
    280280
    281281                                cs->dir = opendir(*cs->path);
     282
     283                                /* Skip directories that we fail to open. */
     284                                if (cs->dir == NULL)
     285                                        cs->path++;
    282286                        }
    283287
  • uspace/app/bdsh/exec.c

    refcebe1 r3fb0fec  
    5252static int try_access(const char *);
    5353
    54 const char *search_dir[] = { "app", "srv", NULL };
     54const char *search_dir[] = { "/app", "/srv", NULL };
    5555
    5656/* work-around for access() */
  • uspace/app/bdsh/input.c

    refcebe1 r3fb0fec  
    170170        }
    171171       
    172         rc = run_command(cmd, usr, &new_iostate);
     172        rc = run_command(actual_cmd, usr, &new_iostate);
    173173       
    174174finit_with_files:
  • uspace/app/init/init.c

    refcebe1 r3fb0fec  
    294294       
    295295#ifdef CONFIG_MOUNT_DATA
     296        /* Make sure fat is running. */
     297        if (str_cmp(STRING(RDFMT), "fat") != 0) {
     298                srv_start("/srv/fat");
     299        }
    296300        mount_data();
    297301#else
  • uspace/app/taskdump/elf_core.c

    refcebe1 r3fb0fec  
    11/*
    2  * Copyright (c) 2010 Jiri Svoboda
     2 * Copyright (c) 2011 Jiri Svoboda
    33 * All rights reserved.
    44 *
     
    3838 * Looking at core files produced by Linux, these don't have section headers,
    3939 * only program headers, although objdump shows them as having sections.
    40  * Basically at the beginning there should be a note segment (which we
    41  * do not write) and one loadable segment per memory area (which we do write).
    42  *
    43  * The note segment probably contains register state, etc. -- we don't
    44  * deal with these yet. Nevertheless you can use these core files with
    45  * objdump or gdb.
    46  */
    47 
     40 * Basically at the beginning there should be a note segment followed
     41 * by one loadable segment per memory area.
     42 *
     43 * The note segment contains a series of records with register state,
     44 * process info etc. We only write one record NT_PRSTATUS which contains
     45 * process/register state (anything which is not register state we fill
     46 * with zeroes).
     47 */
     48
     49#include <align.h>
     50#include <elf/elf.h>
     51#include <elf/elf_linux.h>
    4852#include <stdio.h>
    4953#include <stdlib.h>
     
    5862#include <udebug.h>
    5963#include <macros.h>
    60 
    61 #include <elf.h>
    62 #include "include/elf_core.h"
     64#include <libarch/istate.h>
     65
     66#include "elf_core.h"
    6367
    6468static off64_t align_foff_up(off64_t, uintptr_t, size_t);
    65 static int write_all(int, void *, size_t);
     69static int align_pos(int, size_t);
    6670static int write_mem_area(int, as_area_info_t *, async_sess_t *);
    6771
     
    8387 */
    8488int elf_core_save(const char *file_name, as_area_info_t *ainfo, unsigned int n,
    85     async_sess_t *sess)
     89    async_sess_t *sess, istate_t *istate)
    8690{
    8791        elf_header_t elf_hdr;
     
    9094        elf_word flags;
    9195        elf_segment_header_t *p_hdr;
     96        elf_prstatus_t pr_status;
     97        elf_note_t note;
     98        size_t word_size;
    9299
    93100        int fd;
    94         int rc;
     101        ssize_t rc;
    95102        unsigned int i;
    96103
    97         n_ph = n;
    98 
    99         p_hdr = malloc(sizeof(elf_segment_header_t) * n);
     104#ifdef __32_BITS__
     105        word_size = 4;
     106#endif
     107#ifdef __64_BITS__
     108        word_size = 8;
     109#endif
     110        memset(&pr_status, 0, sizeof(pr_status));
     111        istate_to_elf_regs(istate, &pr_status.regs);
     112
     113        n_ph = n + 1;
     114
     115        p_hdr = malloc(sizeof(elf_segment_header_t) * n_ph);
    100116        if (p_hdr == NULL) {
    101117                printf("Failed allocating memory.\n");
     
    115131         *      ELF header
    116132         *      program headers
     133         *      note segment
    117134         * repeat:
    118135         *      (pad for alignment)
    119          *      segment data
     136         *      core segment
    120137         * end repeat
    121138         */
     
    147164        foff = elf_hdr.e_phoff + n_ph * sizeof(elf_segment_header_t);
    148165
    149         for (i = 1; i <= n; ++i) {
    150                 foff = align_foff_up(foff, ainfo[i - 1].start_addr, PAGE_SIZE);
     166        memset(&p_hdr[0], 0, sizeof(p_hdr[0]));
     167        p_hdr[0].p_type = PT_NOTE;
     168        p_hdr[0].p_offset = foff;
     169        p_hdr[0].p_vaddr = 0;
     170        p_hdr[0].p_paddr = 0;
     171        p_hdr[0].p_filesz = sizeof(elf_note_t)
     172            + ALIGN_UP((str_size("CORE") + 1), word_size)
     173            + ALIGN_UP(sizeof(elf_prstatus_t), word_size);
     174        p_hdr[0].p_memsz = 0;
     175        p_hdr[0].p_flags = 0;
     176        p_hdr[0].p_align = 1;
     177
     178        foff += p_hdr[0].p_filesz;
     179
     180        for (i = 0; i < n; ++i) {
     181                foff = align_foff_up(foff, ainfo[i].start_addr, PAGE_SIZE);
    151182
    152183                flags = 0;
    153                 if (ainfo[i - 1].flags & AS_AREA_READ)
     184                if (ainfo[i].flags & AS_AREA_READ)
    154185                        flags |= PF_R;
    155                 if (ainfo[i - 1].flags & AS_AREA_WRITE)
     186                if (ainfo[i].flags & AS_AREA_WRITE)
    156187                        flags |= PF_W;
    157                 if (ainfo[i - 1].flags & AS_AREA_EXEC)
     188                if (ainfo[i].flags & AS_AREA_EXEC)
    158189                        flags |= PF_X;
    159190
    160                 memset(&p_hdr[i - 1], 0, sizeof(p_hdr[i - 1]));
    161                 p_hdr[i - 1].p_type = PT_LOAD;
    162                 p_hdr[i - 1].p_offset = foff;
    163                 p_hdr[i - 1].p_vaddr = ainfo[i - 1].start_addr;
    164                 p_hdr[i - 1].p_paddr = 0;
    165                 p_hdr[i - 1].p_filesz = ainfo[i - 1].size;
    166                 p_hdr[i - 1].p_memsz = ainfo[i - 1].size;
    167                 p_hdr[i - 1].p_flags = flags;
    168                 p_hdr[i - 1].p_align = PAGE_SIZE;
    169 
    170                 foff += ainfo[i - 1].size;
     191                memset(&p_hdr[i + 1], 0, sizeof(p_hdr[i + 1]));
     192                p_hdr[i + 1].p_type = PT_LOAD;
     193                p_hdr[i + 1].p_offset = foff;
     194                p_hdr[i + 1].p_vaddr = ainfo[i].start_addr;
     195                p_hdr[i + 1].p_paddr = 0;
     196                p_hdr[i + 1].p_filesz = ainfo[i].size;
     197                p_hdr[i + 1].p_memsz = ainfo[i].size;
     198                p_hdr[i + 1].p_flags = flags;
     199                p_hdr[i + 1].p_align = PAGE_SIZE;
     200
     201                foff += ainfo[i].size;
    171202        }
    172203
    173204        rc = write_all(fd, &elf_hdr, sizeof(elf_hdr));
    174         if (rc != EOK) {
     205        if (rc != sizeof(elf_hdr)) {
    175206                printf("Failed writing ELF header.\n");
    176207                free(p_hdr);
     
    180211        for (i = 0; i < n_ph; ++i) {
    181212                rc = write_all(fd, &p_hdr[i], sizeof(p_hdr[i]));
    182                 if (rc != EOK) {
     213                if (rc != sizeof(p_hdr[i])) {
    183214                        printf("Failed writing program header.\n");
    184215                        free(p_hdr);
     
    187218        }
    188219
    189         for (i = 0; i < n_ph; ++i) {
     220        if (lseek(fd, p_hdr[0].p_offset, SEEK_SET) == (off64_t) -1) {
     221                printf("Failed writing memory data.\n");
     222                free(p_hdr);
     223                return EIO;
     224        }
     225
     226        /*
     227         * Write note header
     228         */
     229        note.namesz = str_size("CORE") + 1;
     230        note.descsz = sizeof(elf_prstatus_t);
     231        note.type = NT_PRSTATUS;
     232
     233        rc = write_all(fd, &note, sizeof(elf_note_t));
     234        if (rc != sizeof(elf_note_t)) {
     235                printf("Failed writing note header.\n");
     236                free(p_hdr);
     237                return EIO;
     238        }
     239
     240        rc = write_all(fd, "CORE", note.namesz);
     241        if (rc != (ssize_t) note.namesz) {
     242                printf("Failed writing note header.\n");
     243                free(p_hdr);
     244                return EIO;
     245        }
     246
     247        rc = align_pos(fd, word_size);
     248        if (rc != EOK) {
     249                printf("Failed writing note header.\n");
     250                free(p_hdr);
     251                return EIO;
     252        }
     253
     254        rc = write_all(fd, &pr_status, sizeof(elf_prstatus_t));
     255        if (rc != sizeof(elf_prstatus_t)) {
     256                printf("Failed writing register data.\n");
     257                free(p_hdr);
     258                return EIO;
     259        }
     260
     261        for (i = 1; i < n_ph; ++i) {
    190262                if (lseek(fd, p_hdr[i].p_offset, SEEK_SET) == (off64_t) -1) {
    191263                        printf("Failed writing memory data.\n");
     
    193265                        return EIO;
    194266                }
    195                 if (write_mem_area(fd, &ainfo[i], sess) != EOK) {
     267                if (write_mem_area(fd, &ainfo[i - 1], sess) != EOK) {
    196268                        printf("Failed writing memory data.\n");
    197269                        free(p_hdr);
     
    210282        off64_t rva = vaddr % page_size;
    211283        off64_t rfo = foff % page_size;
    212        
     284
    213285        if (rva >= rfo)
    214286                return (foff + (rva - rfo));
    215        
     287
    216288        return (foff + (page_size + (rva - rfo)));
    217289}
     
    231303        size_t total;
    232304        uintptr_t addr;
    233         int rc;
     305        ssize_t rc;
    234306
    235307        addr = area->start_addr;
     
    245317
    246318                rc = write_all(fd, buffer, to_copy);
    247                 if (rc != EOK) {
     319                if (rc != (ssize_t) to_copy) {
    248320                        printf("Failed writing memory contents.\n");
    249321                        return EIO;
     
    257329}
    258330
    259 /** Write until the buffer is written in its entirety.
    260  *
    261  * This function fails if it cannot write exactly @a len bytes to the file.
    262  *
    263  * @param fd            The file to write to.
    264  * @param buf           Data, @a len bytes long.
    265  * @param len           Number of bytes to write.
    266  *
    267  * @return              EOK on error, return value from write() if writing
    268  *                      failed.
    269  */
    270 static int write_all(int fd, void *data, size_t len)
     331static int align_pos(int fd, size_t align)
    271332{
    272         int cnt = 0;
    273 
    274         do {
    275                 data += cnt;
    276                 len -= cnt;
    277                 cnt = write(fd, data, len);
    278         } while (cnt > 0 && (len - cnt) > 0);
    279 
    280         if (cnt < 0)
    281                 return cnt;
    282 
    283         if (len - cnt > 0)
    284                 return EIO;
     333        off64_t cur_pos;
     334        size_t rem, adv;
     335
     336        cur_pos = lseek(fd, 0, SEEK_CUR);
     337        if (cur_pos < 0)
     338                return -1;
     339
     340        rem = cur_pos % align;
     341        adv = align - rem;
     342
     343        cur_pos = lseek(fd, adv, SEEK_CUR);
     344        if (cur_pos < 0)
     345                return -1;
    285346
    286347        return EOK;
    287348}
    288349
    289 
    290350/** @}
    291351 */
  • uspace/app/taskdump/include/elf_core.h

    refcebe1 r3fb0fec  
    3737
    3838#include <async.h>
     39#include <elf/elf_linux.h>
     40#include <libarch/istate.h>
    3941
    4042extern int elf_core_save(const char *, as_area_info_t *, unsigned int,
    41     async_sess_t *);
     43    async_sess_t *, istate_t *);
    4244
    4345#endif
  • uspace/app/taskdump/include/symtab.h

    refcebe1 r3fb0fec  
    3636#define SYMTAB_H_
    3737
     38#include <elf/elf.h>
    3839#include <sys/types.h>
    39 #include <elf.h>
    4040
    4141typedef struct {
  • uspace/app/taskdump/symtab.c

    refcebe1 r3fb0fec  
    3636 */
    3737
     38#include <elf/elf.h>
    3839#include <stdio.h>
    3940#include <stdlib.h>
     
    4344#include <fcntl.h>
    4445
    45 #include <elf.h>
    4646#include "include/symtab.h"
    4747
     
    5050    elf_section_header_t *shdr);
    5151static int chunk_load(int fd, off64_t start, size_t size, void **ptr);
    52 static int read_all(int fd, void *buf, size_t len);
    5352
    5453/** Load symbol table from an ELF file.
     
    9089
    9190        rc = read_all(fd, &elf_hdr, sizeof(elf_header_t));
    92         if (rc != EOK) {
     91        if (rc != sizeof(elf_header_t)) {
    9392                printf("failed reading elf header\n");
    9493                free(stab);
     
    312311
    313312        rc = read_all(fd, sec_hdr, sizeof(elf_section_header_t));
    314         if (rc != EOK)
     313        if (rc != sizeof(elf_section_header_t))
    315314                return EIO;
    316315
     
    331330static int chunk_load(int fd, off64_t start, size_t size, void **ptr)
    332331{
    333         int rc;
    334 
    335         rc = lseek(fd, start, SEEK_SET);
    336         if (rc == (off64_t) -1) {
     332        ssize_t rc;
     333        off64_t offs;
     334
     335        offs = lseek(fd, start, SEEK_SET);
     336        if (offs == (off64_t) -1) {
    337337                printf("failed seeking chunk\n");
    338338                *ptr = NULL;
     
    347347
    348348        rc = read_all(fd, *ptr, size);
    349         if (rc != EOK) {
     349        if (rc != (ssize_t) size) {
    350350                printf("failed reading chunk\n");
    351351                free(*ptr);
     
    357357}
    358358
    359 /** Read until the buffer is read in its entirety.
    360  *
    361  * This function fails if it cannot read exactly @a len bytes from the file.
    362  *
    363  * @param fd            The file to read from.
    364  * @param buf           Buffer for storing data, @a len bytes long.
    365  * @param len           Number of bytes to read.
    366  *
    367  * @return              EOK on error, EIO if file is short or return value
    368  *                      from read() if reading failed.
    369  */
    370 static int read_all(int fd, void *buf, size_t len)
    371 {
    372         int cnt = 0;
    373 
    374         do {
    375                 buf += cnt;
    376                 len -= cnt;
    377                 cnt = read(fd, buf, len);
    378         } while (cnt > 0 && (len - cnt) > 0);
    379 
    380         if (cnt < 0)
    381                 return cnt;
    382 
    383         if (len - cnt > 0)
    384                 return EIO;
    385 
    386         return EOK;
    387 }
    388 
    389359/** @}
    390360 */
  • uspace/app/taskdump/taskdump.c

    refcebe1 r3fb0fec  
    3434
    3535#include <async.h>
     36#include <elf/elf_linux.h>
    3637#include <stdio.h>
    3738#include <stdlib.h>
     
    7273static char *get_app_task_name(void);
    7374static char *fmt_sym_address(uintptr_t addr);
     75
     76static istate_t reg_state;
    7477
    7578int main(int argc, char *argv[])
     
    293296        if (write_core_file) {
    294297                printf("Writing core file '%s'\n", core_file_name);
    295                 rc = elf_core_save(core_file_name, ainfo_buf, n_areas, sess);
     298
     299                rc = elf_core_save(core_file_name, ainfo_buf, n_areas, sess,
     300                    &reg_state);
     301
    296302                if (rc != EOK) {
    297303                        printf("Failed writing core file.\n");
     
    321327        pc = istate_get_pc(&istate);
    322328        fp = istate_get_fp(&istate);
     329
     330        /* Save register state for dumping to core file later. */
     331        reg_state = istate;
    323332
    324333        sym_pc = fmt_sym_address(pc);
  • uspace/drv/bus/usb/usbmast/bo_trans.c

    refcebe1 r3fb0fec  
    5858 * @param tag           Command block wrapper tag (automatically compared
    5959 *                      with answer)
    60  * @param cmd           Command block
    61  * @param cmd_size      Command block size in bytes
    62  * @param ddir          Direction in which data will be transferred
    63  * @param dbuf          Data send/receive buffer
    64  * @param dbuf_size     Size of the data buffer
    65  * @param xferred_size  Number of bytes actually transferred
     60 * @param cmd           SCSI command
    6661 *
    6762 * @return              Error code
    6863 */
    69 static int usb_massstor_cmd(usbmast_fun_t *mfun, uint32_t tag, const void *cmd,
    70     size_t cmd_size, usb_direction_t ddir, void *dbuf, size_t dbuf_size,
    71     size_t *xferred_size)
     64int usb_massstor_cmd(usbmast_fun_t *mfun, uint32_t tag, scsi_cmd_t *cmd)
    7265{
    7366        int rc;
     67        int retval = EOK;
    7468        size_t act_size;
    7569        usb_pipe_t *bulk_in_pipe = mfun->mdev->usb_dev->pipes[BULK_IN_EP].pipe;
    7670        usb_pipe_t *bulk_out_pipe = mfun->mdev->usb_dev->pipes[BULK_OUT_EP].pipe;
     71        usb_direction_t ddir;
     72        void *dbuf;
     73        size_t dbuf_size;
     74
     75        if (cmd->data_out != NULL && cmd->data_in == NULL) {
     76                ddir = USB_DIRECTION_OUT;
     77                dbuf = (void *)cmd->data_out;
     78                dbuf_size = cmd->data_out_size;
     79        } else if (cmd->data_out == NULL && cmd->data_in != NULL) {
     80                ddir = USB_DIRECTION_IN;
     81                dbuf = cmd->data_in;
     82                dbuf_size = cmd->data_in_size;
     83        } else {
     84                assert(false);
     85        }
    7786
    7887        /* Prepare CBW - command block wrapper */
    7988        usb_massstor_cbw_t cbw;
    8089        usb_massstor_cbw_prepare(&cbw, tag, dbuf_size, ddir, mfun->lun,
    81             cmd_size, cmd);
     90            cmd->cdb_size, cmd->cdb);
    8291
    8392        /* Send the CBW. */
     93        MASTLOG("Sending CBW.\n");
    8494        rc = usb_pipe_write(bulk_out_pipe, &cbw, sizeof(cbw));
    8595        MASTLOG("CBW '%s' sent: %s.\n",
    8696            usb_debug_str_buffer((uint8_t *) &cbw, sizeof(cbw), 0),
    8797            str_error(rc));
    88         if (rc != EOK) {
    89                 return rc;
    90         }
    91 
     98        if (rc != EOK)
     99                return EIO;
     100
     101        MASTLOG("Transferring data.\n");
    92102        if (ddir == USB_DIRECTION_IN) {
    93103                /* Recieve data from the device. */
     
    104114        }
    105115
    106         if (rc != EOK) {
    107                 /*
    108                  * XXX If the pipe is stalled, we should clear it
    109                  * and read CSW.
    110                  */
    111                 return rc;
     116        if (rc == ESTALL) {
     117                /* Clear stall condition and continue below to read CSW. */
     118                if (ddir == USB_DIRECTION_IN) {
     119                        usb_pipe_clear_halt(&mfun->mdev->usb_dev->ctrl_pipe,
     120                            mfun->mdev->usb_dev->pipes[BULK_IN_EP].pipe);
     121                } else {
     122                        usb_pipe_clear_halt(&mfun->mdev->usb_dev->ctrl_pipe,
     123                            mfun->mdev->usb_dev->pipes[BULK_OUT_EP].pipe);
     124                }
     125        } else if (rc != EOK) {
     126                return EIO;
    112127        }
    113128
     
    115130        usb_massstor_csw_t csw;
    116131        size_t csw_size;
     132        MASTLOG("Reading CSW.\n");
    117133        rc = usb_pipe_read(bulk_in_pipe, &csw, sizeof(csw), &csw_size);
    118134        MASTLOG("CSW '%s' received (%zu bytes): %s.\n",
     
    121137        if (rc != EOK) {
    122138                MASTLOG("rc != EOK\n");
    123                 return rc;
     139                return EIO;
    124140        }
    125141
    126142        if (csw_size != sizeof(csw)) {
    127143                MASTLOG("csw_size != sizeof(csw)\n");
    128                 return ERANGE;
     144                return EIO;
    129145        }
    130146
    131147        if (csw.dCSWTag != tag) {
    132148                MASTLOG("csw.dCSWTag != tag\n");
    133                 return EBADCHECKSUM;
     149                return EIO;
    134150        }
    135151
     
    137153         * Determine the actual return value from the CSW.
    138154         */
    139         if (csw.dCSWStatus != 0) {
    140                 MASTLOG("csw.dCSWStatus != 0\n");
    141                 // FIXME: better error code
    142                 // FIXME: distinguish 0x01 and 0x02
    143                 return EXDEV;
     155        switch (csw.dCSWStatus) {
     156        case cbs_passed:
     157                cmd->status = CMDS_GOOD;
     158                break;
     159        case cbs_failed:
     160                MASTLOG("Command failed\n");
     161                cmd->status = CMDS_FAILED;
     162                break;
     163        case cbs_phase_error:
     164                MASTLOG("Phase error\n");
     165                retval = EIO;
     166                break;
     167        default:
     168                retval = EIO;
     169                break;
    144170        }
    145171
     
    147173        if (residue > dbuf_size) {
    148174                MASTLOG("residue > dbuf_size\n");
    149                 return ERANGE;
     175                return EIO;
    150176        }
    151177
     
    158184         */
    159185
    160         if (xferred_size != NULL)
    161                 *xferred_size = dbuf_size - residue;
    162 
    163         return EOK;
    164 }
    165 
    166 /** Perform data-in command.
    167  *
    168  * @param mfun          Mass storage function
    169  * @param tag           Command block wrapper tag (automatically compared with
    170  *                      answer)
    171  * @param cmd           CDB (Command Descriptor)
    172  * @param cmd_size      CDB length in bytes
    173  * @param dbuf          Data receive buffer
    174  * @param dbuf_size     Data receive buffer size in bytes
    175  * @param proc_size     Number of bytes actually processed by device
    176  *
    177  * @return Error code
    178  */
    179 int usb_massstor_data_in(usbmast_fun_t *mfun, uint32_t tag, const void *cmd,
    180     size_t cmd_size, void *dbuf, size_t dbuf_size, size_t *proc_size)
    181 {
    182         return usb_massstor_cmd(mfun, tag, cmd, cmd_size, USB_DIRECTION_IN,
    183             dbuf, dbuf_size, proc_size);
    184 }
    185 
    186 /** Perform data-out command.
    187  *
    188  * @param mfun          Mass storage function
    189  * @param tag           Command block wrapper tag (automatically compared with
    190  *                      answer)
    191  * @param cmd           CDB (Command Descriptor)
    192  * @param cmd_size      CDB length in bytes
    193  * @param data          Command data
    194  * @param data_size     Size of @a data in bytes
    195  * @param proc_size     Number of bytes actually processed by device
    196  *
    197  * @return Error code
    198  */
    199 int usb_massstor_data_out(usbmast_fun_t *mfun, uint32_t tag, const void *cmd,
    200     size_t cmd_size, const void *data, size_t data_size, size_t *proc_size)
    201 {
    202         return usb_massstor_cmd(mfun, tag, cmd, cmd_size, USB_DIRECTION_OUT,
    203             (void *) data, data_size, proc_size);
     186        if (ddir == USB_DIRECTION_IN)
     187                cmd->rcvd_size = dbuf_size - residue;
     188
     189        return retval;
    204190}
    205191
  • uspace/drv/bus/usb/usbmast/bo_trans.h

    refcebe1 r3fb0fec  
    4747#define BULK_OUT_EP 1
    4848
    49 extern int usb_massstor_data_in(usbmast_fun_t *, uint32_t, const void *,
    50     size_t, void *, size_t, size_t *);
    51 extern int usb_massstor_data_out(usbmast_fun_t *, uint32_t, const void *,
    52     size_t, const void *, size_t, size_t *);
     49typedef enum cmd_status {
     50        CMDS_GOOD,
     51        CMDS_FAILED
     52} cmd_status_t;
     53
     54/** SCSI command.
     55 *
     56 * Contains (a subset of) the input and output arguments of SCSI
     57 * Execute Command procedure call (see SAM-4 chapter 5.1)
     58 */
     59typedef struct {
     60        /*
     61         * Related to IN fields
     62         */
     63
     64        /** Command Descriptor Block */
     65        void *cdb;
     66        /** CDB size in bytes */
     67        size_t cdb_size;
     68
     69        /** Outgoing data */
     70        const void *data_out;
     71        /** Size of outgoing data in bytes */
     72        size_t data_out_size;
     73
     74        /*
     75         * Related to OUT fields
     76         */
     77
     78        /** Buffer for incoming data */
     79        void *data_in;
     80        /** Size of input buffer in bytes */
     81        size_t data_in_size;
     82
     83        /** Number of bytes actually received */
     84        size_t rcvd_size;
     85
     86        /** Status */
     87        cmd_status_t status;
     88} scsi_cmd_t;
     89
     90extern int usb_massstor_cmd(usbmast_fun_t *, uint32_t, scsi_cmd_t *);
    5391extern int usb_massstor_reset(usbmast_dev_t *);
    5492extern void usb_massstor_reset_recovery(usbmast_dev_t *);
  • uspace/drv/bus/usb/usbmast/cmdw.h

    refcebe1 r3fb0fec  
    5757} __attribute__((packed)) usb_massstor_csw_t;
    5858
     59enum cmd_block_status {
     60        cbs_passed      = 0x00,
     61        cbs_failed      = 0x01,
     62        cbs_phase_error = 0x02
     63};
     64
    5965extern void usb_massstor_cbw_prepare(usb_massstor_cbw_t *, uint32_t, uint32_t,
    6066    usb_direction_t, uint8_t, uint8_t, const uint8_t *);
  • uspace/drv/bus/usb/usbmast/scsi_ms.c

    refcebe1 r3fb0fec  
    6161}
    6262
     63static void usbmast_dump_sense(scsi_sense_data_t *sense_buf)
     64{
     65        unsigned sense_key;
     66
     67        sense_key = sense_buf->flags_key & 0x0f;
     68        printf("Got sense data. Sense key: 0x%x (%s), ASC 0x%02x, "
     69            "ASCQ 0x%02x.\n", sense_key,
     70            scsi_get_sense_key_str(sense_key),
     71            sense_buf->additional_code,
     72            sense_buf->additional_cqual);
     73}
     74
     75/** Run SCSI command.
     76 *
     77 * Run command and repeat in case of unit attention.
     78 * XXX This is too simplified.
     79 */
     80static int usbmast_run_cmd(usbmast_fun_t *mfun, scsi_cmd_t *cmd)
     81{
     82        uint8_t sense_key;
     83        scsi_sense_data_t sense_buf;
     84        int rc;
     85
     86        do {
     87                rc = usb_massstor_cmd(mfun, 0xDEADBEEF, cmd);
     88                if (rc != EOK) {
     89                        usb_log_error("Inquiry transport failed, device %s: %s.\n",
     90                           mfun->mdev->ddf_dev->name, str_error(rc));
     91                        return rc;
     92                }
     93
     94                if (cmd->status == CMDS_GOOD)
     95                        return EOK;
     96
     97                usb_log_error("SCSI command failed, device %s.\n",
     98                    mfun->mdev->ddf_dev->name);
     99
     100                rc = usbmast_request_sense(mfun, &sense_buf, sizeof(sense_buf));
     101                if (rc != EOK) {
     102                        usb_log_error("Failed to read sense data.\n");
     103                        return EIO;
     104                }
     105
     106                /* Dump sense data to log */
     107                usbmast_dump_sense(&sense_buf);
     108
     109                /* Get sense key */
     110                sense_key = sense_buf.flags_key & 0x0f;
     111
     112                if (sense_key == SCSI_SK_UNIT_ATTENTION) {
     113                        printf("Got unit attention. Re-trying command.\n");
     114                }
     115
     116        } while (sense_key == SCSI_SK_UNIT_ATTENTION);
     117
     118        /* Command status is not good, nevertheless transport succeeded. */
     119        return EOK;
     120}
     121
    63122/** Perform SCSI Inquiry command on USB mass storage device.
    64123 *
     
    70129{
    71130        scsi_std_inquiry_data_t inq_data;
    72         size_t response_len;
     131        scsi_cmd_t cmd;
    73132        scsi_cdb_inquiry_t cdb;
    74133        int rc;
     
    78137        cdb.alloc_len = host2uint16_t_be(sizeof(inq_data));
    79138
    80         rc = usb_massstor_data_in(mfun, 0xDEADBEEF, (uint8_t *) &cdb,
    81             sizeof(cdb), &inq_data, sizeof(inq_data), &response_len);
     139        memset(&cmd, 0, sizeof(cmd));
     140        cmd.cdb = &cdb;
     141        cmd.cdb_size = sizeof(cdb);
     142        cmd.data_in = &inq_data;
     143        cmd.data_in_size = sizeof(inq_data);
     144
     145        rc = usb_massstor_cmd(mfun, 0xDEADBEEF, &cmd);
    82146
    83147        if (rc != EOK) {
    84                 usb_log_error("Inquiry failed, device %s: %s.\n",
    85                    mfun->mdev->ddf_dev->name, str_error(rc));
    86                 return rc;
    87         }
    88 
    89         if (response_len < SCSI_STD_INQUIRY_DATA_MIN_SIZE) {
     148                usb_log_error("Inquiry transport failed, device %s: %s.\n",
     149                   mfun->mdev->ddf_dev->name, str_error(rc));
     150                return rc;
     151        }
     152
     153        if (cmd.status != CMDS_GOOD) {
     154                usb_log_error("Inquiry command failed, device %s.\n",
     155                   mfun->mdev->ddf_dev->name);
     156                return EIO;
     157        }
     158
     159        if (cmd.rcvd_size < SCSI_STD_INQUIRY_DATA_MIN_SIZE) {
    90160                usb_log_error("SCSI Inquiry response too short (%zu).\n",
    91                     response_len);
     161                    cmd.rcvd_size);
    92162                return EIO;
    93163        }
     
    127197int usbmast_request_sense(usbmast_fun_t *mfun, void *buf, size_t size)
    128198{
     199        scsi_cmd_t cmd;
    129200        scsi_cdb_request_sense_t cdb;
    130         size_t data_len;
    131201        int rc;
    132202
     
    135205        cdb.alloc_len = min(size, SCSI_SENSE_DATA_MAX_SIZE);
    136206
    137         rc = usb_massstor_data_in(mfun, 0xDEADBEEF, (uint8_t *) &cdb,
    138             sizeof(cdb), buf, size, &data_len);
    139 
    140         if (rc != EOK) {
     207        memset(&cmd, 0, sizeof(cmd));
     208        cmd.cdb = &cdb;
     209        cmd.cdb_size = sizeof(cdb);
     210        cmd.data_in = buf;
     211        cmd.data_in_size = size;
     212
     213        rc = usb_massstor_cmd(mfun, 0xDEADBEEF, &cmd);
     214
     215        if (rc != EOK || cmd.status != CMDS_GOOD) {
    141216                usb_log_error("Request Sense failed, device %s: %s.\n",
    142217                   mfun->mdev->ddf_dev->name, str_error(rc));
     
    144219        }
    145220
    146         if (data_len < SCSI_SENSE_DATA_MIN_SIZE) {
     221        if (cmd.rcvd_size < SCSI_SENSE_DATA_MIN_SIZE) {
    147222                /* The missing bytes should be considered to be zeroes. */
    148                 memset((uint8_t *)buf + data_len, 0,
    149                     SCSI_SENSE_DATA_MIN_SIZE - data_len);
     223                memset((uint8_t *)buf + cmd.rcvd_size, 0,
     224                    SCSI_SENSE_DATA_MIN_SIZE - cmd.rcvd_size);
    150225        }
    151226
     
    164239    uint32_t *block_size)
    165240{
     241        scsi_cmd_t cmd;
    166242        scsi_cdb_read_capacity_10_t cdb;
    167243        scsi_read_capacity_10_data_t data;
    168         size_t data_len;
    169244        int rc;
    170245
     
    172247        cdb.op_code = SCSI_CMD_READ_CAPACITY_10;
    173248
    174         rc = usb_massstor_data_in(mfun, 0xDEADBEEF, (uint8_t *) &cdb,
    175             sizeof(cdb), &data, sizeof(data), &data_len);
     249        memset(&cmd, 0, sizeof(cmd));
     250        cmd.cdb = &cdb;
     251        cmd.cdb_size = sizeof(cdb);
     252        cmd.data_in = &data;
     253        cmd.data_in_size = sizeof(data);
     254
     255        rc = usbmast_run_cmd(mfun, &cmd);
    176256
    177257        if (rc != EOK) {
    178                 usb_log_error("Read Capacity (10) failed, device %s: %s.\n",
    179                    mfun->mdev->ddf_dev->name, str_error(rc));
    180                 return rc;
    181         }
    182 
    183         if (data_len < sizeof(data)) {
     258                usb_log_error("Read Capacity (10) transport failed, device %s: %s.\n",
     259                   mfun->mdev->ddf_dev->name, str_error(rc));
     260                return rc;
     261        }
     262
     263        if (cmd.status != CMDS_GOOD) {
     264                usb_log_error("Read Capacity (10) command failed, device %s.\n",
     265                   mfun->mdev->ddf_dev->name);
     266                return EIO;
     267        }
     268
     269        if (cmd.rcvd_size < sizeof(data)) {
    184270                usb_log_error("SCSI Read Capacity response too short (%zu).\n",
    185                     data_len);
     271                    cmd.rcvd_size);
    186272                return EIO;
    187273        }
     
    203289int usbmast_read(usbmast_fun_t *mfun, uint64_t ba, size_t nblocks, void *buf)
    204290{
    205         scsi_cdb_read_12_t cdb;
    206         size_t data_len;
    207         int rc;
    208 
    209         /* XXX Need softstate to store block size. */
     291        scsi_cmd_t cmd;
     292        scsi_cdb_read_10_t cdb;
     293        int rc;
    210294
    211295        if (ba > UINT32_MAX)
    212296                return ELIMIT;
    213297
    214         if ((uint64_t)nblocks * mfun->block_size > UINT32_MAX)
     298        if (nblocks > UINT16_MAX)
    215299                return ELIMIT;
    216300
    217301        memset(&cdb, 0, sizeof(cdb));
    218         cdb.op_code = SCSI_CMD_READ_12;
     302        cdb.op_code = SCSI_CMD_READ_10;
    219303        cdb.lba = host2uint32_t_be(ba);
    220         cdb.xfer_len = host2uint32_t_be(nblocks);
    221 
    222         rc = usb_massstor_data_in(mfun, 0xDEADBEEF, (uint8_t *) &cdb,
    223             sizeof(cdb), buf, nblocks * mfun->block_size, &data_len);
     304        cdb.xfer_len = host2uint16_t_be(nblocks);
     305
     306        memset(&cmd, 0, sizeof(cmd));
     307        cmd.cdb = &cdb;
     308        cmd.cdb_size = sizeof(cdb);
     309        cmd.data_in = buf;
     310        cmd.data_in_size = nblocks * mfun->block_size;
     311
     312        rc = usbmast_run_cmd(mfun, &cmd);
    224313
    225314        if (rc != EOK) {
    226                 usb_log_error("Read (12) failed, device %s: %s.\n",
    227                    mfun->mdev->ddf_dev->name, str_error(rc));
    228                 return rc;
    229         }
    230 
    231         if (data_len < nblocks * mfun->block_size) {
     315                usb_log_error("Read (10) transport failed, device %s: %s.\n",
     316                   mfun->mdev->ddf_dev->name, str_error(rc));
     317                return rc;
     318        }
     319
     320        if (cmd.status != CMDS_GOOD) {
     321                usb_log_error("Read (10) command failed, device %s.\n",
     322                   mfun->mdev->ddf_dev->name);
     323                return EIO;
     324        }
     325
     326        if (cmd.rcvd_size < nblocks * mfun->block_size) {
    232327                usb_log_error("SCSI Read response too short (%zu).\n",
    233                     data_len);
     328                    cmd.rcvd_size);
    234329                return EIO;
    235330        }
     
    250345    const void *data)
    251346{
    252         scsi_cdb_write_12_t cdb;
    253         size_t sent_len;
     347        scsi_cmd_t cmd;
     348        scsi_cdb_write_10_t cdb;
    254349        int rc;
    255350
     
    257352                return ELIMIT;
    258353
    259         if ((uint64_t)nblocks * mfun->block_size > UINT32_MAX)
     354        if (nblocks > UINT16_MAX)
    260355                return ELIMIT;
    261356
    262357        memset(&cdb, 0, sizeof(cdb));
    263         cdb.op_code = SCSI_CMD_WRITE_12;
     358        cdb.op_code = SCSI_CMD_WRITE_10;
    264359        cdb.lba = host2uint32_t_be(ba);
    265         cdb.xfer_len = host2uint32_t_be(nblocks);
    266 
    267         rc = usb_massstor_data_out(mfun, 0xDEADBEEF, (uint8_t *) &cdb,
    268             sizeof(cdb), data, nblocks * mfun->block_size, &sent_len);
     360        cdb.xfer_len = host2uint16_t_be(nblocks);
     361
     362        memset(&cmd, 0, sizeof(cmd));
     363        cmd.cdb = &cdb;
     364        cmd.cdb_size = sizeof(cdb);
     365        cmd.data_out = data;
     366        cmd.data_out_size = nblocks * mfun->block_size;
     367
     368        rc = usbmast_run_cmd(mfun, &cmd);
    269369
    270370        if (rc != EOK) {
    271                 usb_log_error("Write (12) failed, device %s: %s.\n",
    272                    mfun->mdev->ddf_dev->name, str_error(rc));
    273                 return rc;
    274         }
    275 
    276         if (sent_len < nblocks * mfun->block_size) {
    277                 usb_log_error("SCSI Write not all bytes transferred (%zu).\n",
    278                     sent_len);
     371                usb_log_error("Write (10) transport failed, device %s: %s.\n",
     372                   mfun->mdev->ddf_dev->name, str_error(rc));
     373                return rc;
     374        }
     375
     376        if (cmd.status != CMDS_GOOD) {
     377                usb_log_error("Write (10) command failed, device %s.\n",
     378                   mfun->mdev->ddf_dev->name);
    279379                return EIO;
    280380        }
  • uspace/lib/c/Makefile

    refcebe1 r3fb0fec  
    5959-include arch/$(UARCH)/Makefile.inc
    6060
    61 EXTRA_CFLAGS += -I../../srv/loader/include
    62 
    6361GENERIC_SOURCES = \
    6462        generic/libc.c \
     
    7169        generic/device/hw_res.c \
    7270        generic/device/char_dev.c \
     71        generic/elf/elf_load.c \
    7372        generic/event.c \
    7473        generic/errno.c \
     
    134133                generic/dlfcn.c \
    135134                generic/rtld/rtld.c \
    136                 generic/rtld/elf_load.c \
    137135                generic/rtld/dynamic.c \
    138136                generic/rtld/module.c \
  • uspace/lib/c/generic/elf/elf_load.c

    refcebe1 r3fb0fec  
    2929 */
    3030
    31 /** @addtogroup generic 
     31/** @addtogroup generic
    3232 * @{
    3333 */
     
    4949#include <assert.h>
    5050#include <as.h>
     51#include <elf/elf.h>
    5152#include <unistd.h>
    5253#include <fcntl.h>
     
    5556#include <entry_point.h>
    5657
    57 #include "elf.h"
    58 #include "elf_load.h"
     58#include <elf/elf_load.h>
    5959
    6060#define DPRINTF(...)
     
    7474static int load_segment(elf_ld_t *elf, elf_segment_header_t *entry);
    7575
    76 /** Read until the buffer is read in its entirety. */
    77 static int my_read(int fd, void *buf, size_t len)
    78 {
    79         int cnt = 0;
    80         do {
    81                 buf += cnt;
    82                 len -= cnt;
    83                 cnt = read(fd, buf, len);
    84         } while ((cnt > 0) && ((len - cnt) > 0));
    85 
    86         return cnt;
    87 }
    88 
    8976/** Load ELF binary from a file.
    9077 *
     
    160147        int i, rc;
    161148
    162         rc = my_read(elf->fd, header, sizeof(elf_header_t));
    163         if (rc < 0) {
     149        rc = read_all(elf->fd, header, sizeof(elf_header_t));
     150        if (rc != sizeof(elf_header_t)) {
    164151                DPRINTF("Read error.\n");
    165152                return EE_INVALID;
     
    222209                        + i * sizeof(elf_segment_header_t), SEEK_SET);
    223210
    224                 rc = my_read(elf->fd, &segment_hdr,
     211                rc = read_all(elf->fd, &segment_hdr,
    225212                    sizeof(elf_segment_header_t));
    226                 if (rc < 0) {
     213                if (rc != sizeof(elf_segment_header_t)) {
    227214                        DPRINTF("Read error.\n");
    228215                        return EE_INVALID;
     
    244231                    + i * sizeof(elf_section_header_t), SEEK_SET);
    245232
    246                 rc = my_read(elf->fd, &section_hdr,
     233                rc = read_all(elf->fd, &section_hdr,
    247234                    sizeof(elf_section_header_t));
    248                 if (rc < 0) {
     235                if (rc != sizeof(elf_section_header_t)) {
    249236                        DPRINTF("Read error.\n");
    250237                        return EE_INVALID;
     
    334321        uintptr_t seg_addr;
    335322        size_t mem_sz;
    336         int rc;
     323        ssize_t rc;
    337324
    338325        bias = elf->bias;
     
    412399                if (now > left) now = left;
    413400
    414                 rc = my_read(elf->fd, dp, now);
    415 
    416                 if (rc < 0) {
     401                rc = read_all(elf->fd, dp, now);
     402
     403                if (rc != (ssize_t) now) {
    417404                        DPRINTF("Read error.\n");
    418405                        return EE_INVALID;
  • uspace/lib/c/generic/io/io.c

    refcebe1 r3fb0fec  
    594594                }
    595595               
    596                 buf += now;
     596                data += now;
    597597                stream->buf_head += now;
    598598                buf_free -= now;
    599599                bytes_left -= now;
    600600                total_written += now;
     601                stream->buf_state = _bs_write;
    601602               
    602603                if (buf_free == 0) {
     
    606607                }
    607608        }
    608        
    609         if (total_written > 0)
    610                 stream->buf_state = _bs_write;
    611609
    612610        if (need_flush)
  • uspace/lib/c/generic/rtld/module.c

    refcebe1 r3fb0fec  
    3535 */
    3636
     37#include <adt/list.h>
     38#include <elf/elf_load.h>
     39#include <fcntl.h>
     40#include <loader/pcb.h>
    3741#include <stdio.h>
    3842#include <stdlib.h>
    3943#include <unistd.h>
    40 #include <fcntl.h>
    41 #include <adt/list.h>
    42 #include <loader/pcb.h>
    4344
    4445#include <rtld/rtld.h>
     
    4748#include <rtld/rtld_arch.h>
    4849#include <rtld/module.h>
    49 #include <elf_load.h>
    5050
    5151/** (Eagerly) process all relocation tables in a module.
     
    9393module_t *module_find(const char *name)
    9494{
    95         link_t *head = &runtime_env->modules_head;
    96 
    97         link_t *cur;
    9895        module_t *m;
    9996        const char *p, *soname;
     
    110107
    111108        /* Traverse list of all modules. Not extremely fast, but simple */
    112         DPRINTF("head = %p\n", head);
    113         for (cur = head->next; cur != head; cur = cur->next) {
     109        list_foreach(runtime_env->modules, cur) {
    114110                DPRINTF("cur = %p\n", cur);
    115111                m = list_get_instance(cur, module_t, modules_link);
     
    177173
    178174        /* Insert into the list of loaded modules */
    179         list_append(&m->modules_link, &runtime_env->modules_head);
     175        list_append(&m->modules_link, &runtime_env->modules);
    180176
    181177        return m;
     
    249245void modules_process_relocs(module_t *start)
    250246{
    251         link_t *head = &runtime_env->modules_head;
    252 
    253         link_t *cur;
    254         module_t *m;
    255 
    256         for (cur = head->next; cur != head; cur = cur->next) {
     247        module_t *m;
     248
     249        list_foreach(runtime_env->modules, cur) {
    257250                m = list_get_instance(cur, module_t, modules_link);
    258251
     
    268261void modules_untag(void)
    269262{
    270         link_t *head = &runtime_env->modules_head;
    271 
    272         link_t *cur;
    273         module_t *m;
    274 
    275         for (cur = head->next; cur != head; cur = cur->next) {
     263        module_t *m;
     264
     265        list_foreach(runtime_env->modules, cur) {
    276266                m = list_get_instance(cur, module_t, modules_link);
    277267                m->bfs_tag = false;
  • uspace/lib/c/generic/rtld/rtld.c

    refcebe1 r3fb0fec  
    4444{
    4545        runtime_env = &rt_env_static;
    46         list_initialize(&runtime_env->modules_head);
     46        list_initialize(&runtime_env->modules);
    4747        runtime_env->next_bias = 0x2000000;
    4848        runtime_env->program = NULL;
  • uspace/lib/c/generic/rtld/symbol.c

    refcebe1 r3fb0fec  
    3838#include <stdlib.h>
    3939
     40#include <elf/elf.h>
    4041#include <rtld/rtld.h>
    4142#include <rtld/rtld_debug.h>
    4243#include <rtld/symbol.h>
    43 #include <elf.h>
    4444
    4545/*
     
    118118        module_t *m, *dm;
    119119        elf_symbol_t *sym, *s;
    120         link_t queue_head;
     120        list_t queue;
    121121        size_t i;
    122122
     
    132132
    133133        /* Insert root (the program) into the queue and tag it */
    134         list_initialize(&queue_head);
     134        list_initialize(&queue);
    135135        start->bfs_tag = true;
    136         list_append(&start->queue_link, &queue_head);
     136        list_append(&start->queue_link, &queue);
    137137
    138138        /* If the symbol is found, it will be stored in 'sym' */
     
    140140
    141141        /* While queue is not empty */
    142         while (!list_empty(&queue_head)) {
     142        while (!list_empty(&queue)) {
    143143                /* Pop first element from the queue */
    144                 m = list_get_instance(queue_head.next, module_t, queue_link);
     144                m = list_get_instance(list_first(&queue), module_t, queue_link);
    145145                list_remove(&m->queue_link);
    146146
     
    162162                        if (dm->bfs_tag == false) {
    163163                                dm->bfs_tag = true;
    164                                 list_append(&dm->queue_link, &queue_head);
     164                                list_append(&dm->queue_link, &queue);
    165165                        }
    166166                }
     
    168168
    169169        /* Empty the queue so that we leave it in a clean state */
    170         while (!list_empty(&queue_head))
    171                 list_remove(queue_head.next);
     170        while (!list_empty(&queue))
     171                list_remove(list_first(&queue));
    172172
    173173        if (!sym) {
  • uspace/lib/c/generic/vfs/vfs.c

    refcebe1 r3fb0fec  
    417417}
    418418
     419/** Read entire buffer.
     420 *
     421 * In face of short reads this function continues reading until either
     422 * the entire buffer is read or no more data is available (at end of file).
     423 *
     424 * @param fildes        File descriptor
     425 * @param buf           Buffer, @a nbytes bytes long
     426 * @param nbytes        Number of bytes to read
     427 *
     428 * @return              On success, positive number of bytes read.
     429 *                      On failure, negative error code from read().
     430 */
     431ssize_t read_all(int fildes, void *buf, size_t nbyte)
     432{
     433        ssize_t cnt = 0;
     434        size_t nread = 0;
     435        uint8_t *bp = (uint8_t *) buf;
     436
     437        do {
     438                bp += cnt;
     439                nread += cnt;
     440                cnt = read(fildes, bp, nbyte - nread);
     441        } while (cnt > 0 && (nbyte - nread - cnt) > 0);
     442
     443        if (cnt < 0)
     444                return cnt;
     445
     446        return nread + cnt;
     447}
     448
     449/** Write entire buffer.
     450 *
     451 * This function fails if it cannot write exactly @a len bytes to the file.
     452 *
     453 * @param fildes        File descriptor
     454 * @param buf           Data, @a nbytes bytes long
     455 * @param nbytes        Number of bytes to write
     456 *
     457 * @return              EOK on error, return value from write() if writing
     458 *                      failed.
     459 */
     460ssize_t write_all(int fildes, const void *buf, size_t nbyte)
     461{
     462        ssize_t cnt = 0;
     463        ssize_t nwritten = 0;
     464        const uint8_t *bp = (uint8_t *) buf;
     465
     466        do {
     467                bp += cnt;
     468                nwritten += cnt;
     469                cnt = write(fildes, bp, nbyte - nwritten);
     470        } while (cnt > 0 && ((ssize_t )nbyte - nwritten - cnt) > 0);
     471
     472        if (cnt < 0)
     473                return cnt;
     474
     475        if ((ssize_t)nbyte - nwritten - cnt > 0)
     476                return EIO;
     477
     478        return nbyte;
     479}
     480
    419481int fsync(int fildes)
    420482{
  • uspace/lib/c/include/elf/elf_load.h

    refcebe1 r3fb0fec  
    11/*
     2 * Copyright (c) 2006 Sergey Bondari
    23 * Copyright (c) 2008 Jiri Svoboda
    34 * All rights reserved.
     
    3839
    3940#include <arch/elf.h>
     41#include <elf/elf.h>
    4042#include <sys/types.h>
    4143#include <loader/pcb.h>
    4244
    43 #include "elf.h"
     45/**
     46 * ELF error return codes
     47 */
     48#define EE_OK                   0       /* No error */
     49#define EE_INVALID              1       /* Invalid ELF image */
     50#define EE_MEMORY               2       /* Cannot allocate address space */
     51#define EE_INCOMPATIBLE         3       /* ELF image is not compatible with current architecture */
     52#define EE_UNSUPPORTED          4       /* Non-supported ELF (e.g. dynamic ELFs) */
     53#define EE_LOADER               5       /* The image is actually a program loader. */
     54#define EE_IRRECOVERABLE        6
    4455
    4556typedef enum {
     
    8293} elf_ld_t;
    8394
    84 int elf_load_file(const char *file_name, size_t so_bias, eld_flags_t flags,
    85     elf_info_t *info);
    86 void elf_create_pcb(elf_info_t *info, pcb_t *pcb);
     95extern const char *elf_error(unsigned int);
     96extern int elf_load_file(const char *, size_t, eld_flags_t, elf_info_t *);
     97extern void elf_create_pcb(elf_info_t *, pcb_t *);
    8798
    8899#endif
  • uspace/lib/c/include/rtld/elf_dyn.h

    refcebe1 r3fb0fec  
    3939#include <sys/types.h>
    4040
    41 #include <elf.h>
     41#include <elf/elf.h>
    4242#include <libarch/rtld/elf_dyn.h>
    4343
  • uspace/lib/c/include/rtld/rtld.h

    refcebe1 r3fb0fec  
    4949
    5050        /** List of all loaded modules including rtld and the program */
    51         link_t modules_head;
     51        list_t modules;
    5252
    5353        /** Temporary hack to place each module at different address. */
  • uspace/lib/c/include/rtld/symbol.h

    refcebe1 r3fb0fec  
    3636#define LIBC_RTLD_SYMBOL_H_
    3737
     38#include <elf/elf.h>
    3839#include <rtld/rtld.h>
    39 #include <elf.h>
    4040
    4141elf_symbol_t *symbol_bfs_find(const char *name, module_t *start, module_t **mod);
  • uspace/lib/c/include/unistd.h

    refcebe1 r3fb0fec  
    6363extern ssize_t read(int, void *, size_t);
    6464
     65extern ssize_t read_all(int, void *, size_t);
     66extern ssize_t write_all(int, const void *, size_t);
     67
    6568extern off64_t lseek(int, off64_t, int);
    6669extern int ftruncate(int, aoff64_t);
  • uspace/lib/scsi/include/scsi/sbc.h

    refcebe1 r3fb0fec  
    5656};
    5757
     58/** SCSI Read (10) command */
     59typedef struct {
     60        /** Operation code (SCSI_CMD_READ_10) */
     61        uint8_t op_code;
     62        /** RdProtect, DPO, FUA, Reserved, FUA_NV, Obsolete */
     63        uint8_t flags;
     64        /** Logical block address */
     65        uint32_t lba;
     66        /** Reserved, Group Number */
     67        uint8_t group_no;
     68        /** Transfer length */
     69        uint16_t xfer_len;
     70        /** Control */
     71        uint8_t control;
     72} __attribute__((packed)) scsi_cdb_read_10_t;
     73
    5874/** SCSI Read (12) command */
    5975typedef struct {
    6076        /** Operation code (SCSI_CMD_READ_12) */
    6177        uint8_t op_code;
    62         /** RdProtect, DPO, FUA, Reserved, FUA_NV, Reserved */
     78        /** RdProtect, DPO, FUA, Reserved, FUA_NV, Obsolete */
    6379        uint8_t flags;
    6480        /** Logical block address */
     
    115131} scsi_read_capacity_10_data_t;
    116132
     133/** SCSI Write (10) command */
     134typedef struct {
     135        /** Operation code (SCSI_CMD_WRITE_10) */
     136        uint8_t op_code;
     137        /** WrProtect, DPO, FUA, Reserved, FUA_NV, Obsolete */
     138        uint8_t flags;
     139        /** Logical block address */
     140        uint32_t lba;
     141        /** Reserved, Group Number */
     142        uint8_t group_no;
     143        /** Transfer length */
     144        uint16_t xfer_len;
     145        /** Control */
     146        uint8_t control;
     147} __attribute__((packed)) scsi_cdb_write_10_t;
     148
    117149/** SCSI Write (12) command */
    118150typedef struct {
    119151        /** Operation code (SCSI_CMD_WRITE_12) */
    120152        uint8_t op_code;
    121         /** WrProtect, DPO, FUA, Reserved, FUA_NV, Reserved */
     153        /** WrProtect, DPO, FUA, Reserved, FUA_NV, Obsolete */
    122154        uint8_t flags;
    123155        /** Logical block address */
  • uspace/lib/scsi/include/scsi/spc.h

    refcebe1 r3fb0fec  
    179179        uint8_t additional_len;
    180180        /** Command-specific Information */
    181         uint8_t cmd_spec;
     181        uint32_t cmd_spec;
    182182        /** Additional Sense Code */
    183183        uint8_t additional_code;
     
    205205        SCSI_SK_ABORTED_COMMAND = 0xb,
    206206        SCSI_SK_VOLUME_OVERFLOW = 0xd,
    207         SCSI_SK_MISCOMPARE      = 0xe
     207        SCSI_SK_MISCOMPARE      = 0xe,
     208
     209        SCSI_SK_LIMIT           = 0x10
    208210};
    209211
    210212extern const char *scsi_dev_type_str[SCSI_DEV_LIMIT];
     213extern const char *scsi_sense_key_str[SCSI_SK_LIMIT];
     214
    211215extern const char *scsi_get_dev_type_str(unsigned);
     216extern const char *scsi_get_sense_key_str(unsigned);
    212217
    213218#endif
  • uspace/lib/scsi/src/spc.c

    refcebe1 r3fb0fec  
    4444};
    4545
     46const char *scsi_sense_key_str[SCSI_SK_LIMIT] = {
     47        [SCSI_SK_NO_SENSE]              = "No Sense",
     48        [SCSI_SK_RECOVERED_ERROR]       = "Recovered Error",
     49        [SCSI_SK_NOT_READY]             = "Not Ready",
     50        [SCSI_SK_MEDIUM_ERROR]          = "Medium Error",
     51        [SCSI_SK_HARDWARE_ERROR]        = "Hardware Error",
     52        [SCSI_SK_ILLEGAL_REQUEST]       = "Illegal Request",
     53        [SCSI_SK_UNIT_ATTENTION]        = "Unit Attention",
     54        [SCSI_SK_DATA_PROTECT]          = "Data Protect",
     55        [SCSI_SK_BLANK_CHECK]           = "Blank Check",
     56        [SCSI_SK_VENDOR_SPECIFIC]       = "Vendor-specific",
     57        [SCSI_SK_COPY_ABORTED]          = "Copy Aborted",
     58        [SCSI_SK_ABORTED_COMMAND]       = "Aborted Command",
     59        [SCSI_SK_VOLUME_OVERFLOW]       = "Volume Overflow",
     60        [SCSI_SK_MISCOMPARE]            = "Miscompare"
     61};
     62
    4663/** Get peripheral device type string.
    4764 *
     
    5370{
    5471        if (dev_type >= SCSI_DEV_LIMIT || scsi_dev_type_str[dev_type] == NULL)
    55                 return "<unknown>";
     72                return "Unknown";
    5673
    5774        return scsi_dev_type_str[dev_type];
    5875}
     76
     77/** Get sense key string.
     78 *
     79 * Return string description of SCSI sense key.
     80 * The returned string is valid indefinitely, the caller should
     81 * not attempt to free it.
     82 */
     83const char *scsi_get_sense_key_str(unsigned sense_key)
     84{
     85        if (sense_key >= SCSI_SK_LIMIT || scsi_sense_key_str[sense_key] == NULL)
     86                return "Unknown";
     87
     88        return scsi_sense_key_str[sense_key];
     89}
     90
  • uspace/srv/devman/devman.c

    refcebe1 r3fb0fec  
    270270        }
    271271       
    272         ssize_t read_bytes = safe_read(fd, buf, len);
     272        ssize_t read_bytes = read_all(fd, buf, len);
    273273        if (read_bytes <= 0) {
    274                 log_msg(LVL_ERROR, "Unable to read file '%s'.", conf_path);
     274                log_msg(LVL_ERROR, "Unable to read file '%s' (%zd).", conf_path,
     275                    read_bytes);
    275276                goto cleanup;
    276277        }
     
    421422        }
    422423       
    423         insert_fun_node(tree, fun, clone_string(""), NULL);
     424        insert_fun_node(tree, fun, str_dup(""), NULL);
    424425        match_id_t *id = create_match_id();
    425         id->id = clone_string("root");
     426        id->id = str_dup("root");
    426427        id->score = 100;
    427428        add_match_id(&fun->match_ids, id);
  • uspace/srv/devman/util.c

    refcebe1 r3fb0fec  
    9191}
    9292
    93 char *clone_string(const char *s)
    94 {
    95         size_t size = str_size(s) + 1;
    96         char *str;
    97        
    98         str = (char *) malloc(size);
    99         if (str != NULL)
    100                 str_cpy(str, size, s);
    101         return str;
    102 }
    103 
    10493void replace_char(char *str, char orig, char repl)
    10594{
     
    111100}
    112101
    113 ssize_t safe_read(int fd, void *buffer, size_t size)
    114 {
    115         if (size == 0) {
    116                 return 0;
    117         }
    118 
    119         uint8_t *buf_ptr = (uint8_t *) buffer;
    120 
    121         size_t total_read = 0;
    122         while (total_read < size) {
    123                 ssize_t bytes_read = read(fd, buf_ptr, size - total_read);
    124                 if (bytes_read < 0) {
    125                         /* Error. */
    126                         return bytes_read;
    127                 } else if (bytes_read == 0) {
    128                         /* Possibly end of file. */
    129                         break;
    130                 } else {
    131                         /* Read at least something. */
    132                         buf_ptr += bytes_read;
    133                         total_read += bytes_read;
    134                 }
    135         }
    136 
    137         return (ssize_t) total_read;
    138 }
    139 
    140102/** @}
    141103 */
  • uspace/srv/devman/util.h

    refcebe1 r3fb0fec  
    4444extern size_t get_nonspace_len(const char *);
    4545extern void free_not_null(const void *);
    46 extern char *clone_string(const char *);
    4746extern void replace_char(char *, char, char);
    48 
    49 extern ssize_t safe_read(int, void *, size_t);
    5047
    5148#endif
  • uspace/srv/loader/Makefile

    refcebe1 r3fb0fec  
    3939LINKER_SCRIPT = $(LIBC_PREFIX)/arch/$(UARCH)/_link-loader.ld
    4040
    41 EXTRA_CFLAGS = -Iinclude
    42 
    4341BINARY = loader
    4442STATIC_ONLY = y
     
    4644GENERIC_SOURCES = \
    4745        main.c \
    48         elf_load.c \
    4946        interp.s
    5047
  • uspace/srv/loader/main.c

    refcebe1 r3fb0fec  
    5959#include <str.h>
    6060#include <as.h>
    61 #include <elf.h>
    62 #include <elf_load.h>
     61#include <elf/elf.h>
     62#include <elf/elf_load.h>
    6363
    6464#ifdef CONFIG_RTLD
     
    348348
    349349        /* Initialize list of loaded modules */
    350         list_initialize(&runtime_env->modules_head);
    351         list_append(&prog_mod.modules_link, &runtime_env->modules_head);
     350        list_initialize(&runtime_env->modules);
     351        list_append(&prog_mod.modules_link, &runtime_env->modules);
    352352
    353353        /* Pointer to program module. Used as root of the module graph. */
Note: See TracChangeset for help on using the changeset viewer.