source: rtems/cpukit/libblock/src/bdpart-register.c

Last change on this file was bcef89f2, checked in by Sebastian Huber <sebastian.huber@…>, on 05/19/23 at 06:18:25

Update company name

The embedded brains GmbH & Co. KG is the legal successor of embedded
brains GmbH.

  • Property mode set to 100644
File size: 5.4 KB
Line 
1/* SPDX-License-Identifier: BSD-2-Clause */
2
3/**
4 * @file
5 *
6 * @ingroup rtems_bdpart
7 *
8 * @brief Block Device Partition Management
9 */
10
11/*
12 * Copyright (C) 2009, 2012 embedded brains GmbH & Co. KG
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
17 * 1. Redistributions of source code must retain the above copyright
18 *    notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 *    notice, this list of conditions and the following disclaimer in the
21 *    documentation and/or other materials provided with the distribution.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
27 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE.
34 */
35
36#ifdef HAVE_CONFIG_H
37#include "config.h"
38#endif
39
40#include <stdio.h>
41#include <stdlib.h>
42#include <string.h>
43#include <unistd.h>
44
45#include <rtems.h>
46#include <rtems/bdpart.h>
47
48static char *create_logical_disk_name( const char *disk_name, char **marker)
49{
50  size_t disk_name_size = strlen( disk_name);
51  char *logical_disk_name = malloc( disk_name_size + RTEMS_BDPART_NUMBER_SIZE);
52
53  if (logical_disk_name != NULL) {
54    /* The string is NUL terminated by (A) ... */
55    memcpy( logical_disk_name, disk_name, disk_name_size);
56    *marker = logical_disk_name + disk_name_size;
57  }
58
59  return logical_disk_name;
60}
61
62static rtems_status_code update_logical_disk_name(
63  char *logical_disk_marker,
64  size_t i
65)
66{
67  rtems_status_code sc = RTEMS_SUCCESSFUL;
68  int rv = 0;
69
70  /* ... (A) */
71  rv = snprintf( logical_disk_marker, RTEMS_BDPART_NUMBER_SIZE, "%zu", i + 1);
72  if (rv >= RTEMS_BDPART_NUMBER_SIZE) {
73    sc = RTEMS_INVALID_NAME;
74  }
75
76  return sc;
77}
78
79rtems_status_code rtems_bdpart_register(
80  const char *disk_name,
81  const rtems_bdpart_partition *pt,
82  size_t count
83)
84{
85  rtems_status_code sc = RTEMS_SUCCESSFUL;
86  rtems_status_code esc = RTEMS_SUCCESSFUL;
87  rtems_blkdev_bnum disk_end = 0;
88  char *logical_disk_name = NULL;
89  char *logical_disk_marker = NULL;
90  size_t i = 0;
91  int fd = -1;
92  rtems_disk_device *dd = NULL;
93
94  /* Get disk data */
95  sc = rtems_bdpart_get_disk_data( disk_name, &fd, &dd, &disk_end);
96  if (sc != RTEMS_SUCCESSFUL) {
97    return sc;
98  }
99
100  /* Create logical disk name */
101  logical_disk_name = create_logical_disk_name(
102    disk_name,
103    &logical_disk_marker
104  );
105  if (logical_disk_name == NULL) {
106    esc = sc;
107    goto cleanup;
108  }
109
110  /* Create a logical disk for each partition */
111  for (i = 0; i < count; ++i) {
112    const rtems_bdpart_partition *p = pt + i;
113
114    /* Set partition number for logical disk name */
115    sc = update_logical_disk_name( logical_disk_marker, i);
116    if (sc != RTEMS_SUCCESSFUL) {
117      esc = sc;
118      goto cleanup;
119    }
120
121    /* Create logical disk */
122    sc = rtems_blkdev_create_partition(
123      logical_disk_name,
124      disk_name,
125      p->begin,
126      p->end - p->begin
127    );
128    if (sc != RTEMS_SUCCESSFUL) {
129      esc = sc;
130      goto cleanup;
131    }
132  }
133
134cleanup:
135
136  free( logical_disk_name);
137  close( fd);
138
139  return esc;
140}
141
142rtems_status_code rtems_bdpart_register_from_disk( const char *disk_name)
143{
144  rtems_status_code sc = RTEMS_SUCCESSFUL;
145  rtems_bdpart_format format;
146  rtems_bdpart_partition pt [RTEMS_BDPART_PARTITION_NUMBER_HINT];
147  size_t count = RTEMS_BDPART_PARTITION_NUMBER_HINT;
148
149  /* Read partitions */
150  sc = rtems_bdpart_read( disk_name, &format, pt, &count);
151  if (sc != RTEMS_SUCCESSFUL) {
152    return sc;
153  }
154
155  /* Register partitions */
156  return rtems_bdpart_register( disk_name, pt, count);
157}
158
159rtems_status_code rtems_bdpart_unregister(
160  const char *disk_name,
161  const rtems_bdpart_partition *pt RTEMS_UNUSED,
162  size_t count
163)
164{
165  rtems_status_code sc = RTEMS_SUCCESSFUL;
166  rtems_status_code esc = RTEMS_SUCCESSFUL;
167  rtems_blkdev_bnum disk_end = 0;
168  char *logical_disk_name = NULL;
169  char *logical_disk_marker = NULL;
170  size_t i = 0;
171  int fd = -1;
172  rtems_disk_device *dd = NULL;
173
174  /* Get disk data */
175  sc = rtems_bdpart_get_disk_data( disk_name, &fd, &dd, &disk_end);
176  if (sc != RTEMS_SUCCESSFUL) {
177    return sc;
178  }
179
180  /* Create logical disk name */
181  logical_disk_name = create_logical_disk_name(
182    disk_name,
183    &logical_disk_marker
184  );
185  if (logical_disk_name == NULL) {
186    esc = sc;
187    goto cleanup;
188  }
189
190  /* Delete the logical disk for each partition */
191  for (i = 0; i < count; ++i) {
192    int rv = 0;
193
194    /* Set partition number for logical disk name */
195    sc = update_logical_disk_name( logical_disk_marker, i);
196    if (sc != RTEMS_SUCCESSFUL) {
197      esc = sc;
198      goto cleanup;
199    }
200
201    /* Delete logical disk */
202    rv = unlink( logical_disk_name);
203    if (rv != 0) {
204      esc = sc;
205      goto cleanup;
206    }
207  }
208
209cleanup:
210
211  free( logical_disk_name);
212  close( fd);
213
214  return esc;
215}
Note: See TracBrowser for help on using the repository browser.